开发者

Error when running helloworld + classpath problem

I am compiling helloworldapp with no errors.

I am running java helloworldapp on windows

CLASSPATH: .;C:\Program Files\Java\jre7\lib;

JAVA_HOME: C:\Program Files\Java\jre7

path includes C:\Program Files\Java\jdk1.7.0\bin\

Result:

C:\Users\k\Desktop\abcl-bin-0.26.2>java helloworldapp
Exception in thread "main" java.lang.NoClassDefFoundError: helloworldapp (wrong
name: HelloWorldApp)
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClass(Unknown Source)
        at java.security.SecureClassLoader.defineClass(Unknown Source)
............etc

So ok, for some reson it couldn't find the classes, so I am trying to do it manually.

C:\Users\k\Desktop\abcl-bin-0.26.2>java -cp "C:\Program Files\Java\jre7\lib" helloworldapp
Error: Could not开发者_如何学Python find or load main class helloworldapp

C:\Users\k\Desktop\abcl-bin-0.26.2>java -cp C:\Program Files\Java\jre7\lib; helloworldapp
Error: Could not find or load main class Files\Java\jre7\lib;

What am I doing wrong? I have looked at several forums, but they weren't particularly helpful.

Update, I got past the helloworld stage. Now the real problem

import org.armedbear.lisp.*;

public class Main
{

    public static void main(String[] argv)
    {
    try
        {
        Main thisObject = new Main();
        Interpreter interpreter = Interpreter.createInstance();
        interpreter.eval("(load \"lispfunctions.lisp\")");
        org.armedbear.lisp.Package defaultPackage = 
            Packages.findPackage("CL-USER");
        Symbol voidsym = 
            defaultPackage.findAccessibleSymbol("VOID-FUNCTION");
        Function voidFunction = (Function) voidsym.getSymbolFunction();
        voidFunction.execute(new JavaObject(thisObject));
        }
    catch (Throwable t)
        {
        System.out.println("exception!");
        t.printStackTrace();
        }
    }
    public int addTwoNumbers(int a, int b)
    {
    return a + b;
    }
}

I tried both

C:\Users\k\Desktop\abcl-bin-0.26.2>java -cp abcl.jar Main
Error: Could not find or load main class Main

C:\Users\k\Desktop\abcl-bin-0.26.2>java -cp abcl.jar main
Error: Could not find or load main class main

The abcl.jar is located in the same folder

Some more trials and errors

C:\Users\k\Desktop\abcl-bin-0.26.2>java -cp .:abcl.jar Main
Error: Could not find or load main class Main

C:\Users\k\Desktop\abcl-bin-0.26.2>java Main -cp .:abcl.jar
Exception in thread "main" java.lang.NoClassDefFoundError: org/armedbear/lisp/Li
spObject
        at java.lang.Class.getDeclaredMethods0(Native Method)
        at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
        at java.lang.Class.getMethod0(Unknown Source)
        at java.lang.Class.getMethod(Unknown Source)
        at sun.launcher.LauncherHelper.getMainMethod(Unknown Source)
        at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
Caused by: java.lang.ClassNotFoundException: org.armedbear.lisp.LispObject
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        ... 6 more


When you write a class in java, the name of the file must match the name of the class.

class HelloWorldApp {
    public static void main(String[] args) {
        System.out.println("Hello world");
    }

}

must be saved as HelloWorldApp.java
It must match the case i.e. helloworldapp.java would not work.

When you need to run the class, you must refer to it using the correct case.

java HelloWorldApp

but not

java helloworldapp

If you are referencing a JAR then you need to add it to your classpath. By default the classpath consists of the files in the current working directory. On windows the classpath is a semi-colon (;) separated list, on Linux and Mac OSX it is a colon (:) separated list.

To include the JAR in the classpath you must run the following...

java -cp .:myjar.jar MyMainClass

where myjar.jar is the JAR file you want to reference and MyMainClass is the class that contains your public static void main method.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜