简体   繁体   中英

Why can't I compile my Java applications using Ubuntu?

I have been trying for what seems like two days now to get my java application to compile from the command line in Ubuntu. I know I have Java installed because I can run my applications in Eclipse & Netbeans and they work fine. But if I want to compile my applications from the command line I get the following error message:

javac Main.java

Everythings fine, no errors or anything. Then I try:

java Main

And I get this error message:

Exception in thread "main" java.lang.NoClassDefFoundError: Main (wrong name: input/Main)
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:637)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:277)
    at java.net.URLClassLoader.access$000(URLClassLoader.java:73)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:212)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:323)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:268)
    at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:336)
Could not find the main class: Main. Program will exit.

Try:

java input.Main

By the looks of your error, your Main class is in package "input". You need to specify package name when running a class, not the filename.

打开终端并粘贴以下命令:

export CLASSPATH=.:/usr/local/tomcat/common/lib/jsp-api.jar:/usr/local/tomcat/common/lib/servlet-api.jar:/home/trenog/javokapi/bin/xmlrpc.jar

This looks like a classic Classpath problem. Eclipse and Netbeans will set up the classpath for you, but when you're writing to the command line, you're on your own.

Assuming you're using BASH, try typing the following into the command line:

CLASSPATH=/path/to/your/java/class/file

Or, alternately, you can do this from the java command line:

java -cp /path/to/your/java/class/file Main

Follow this link for more info.

EDIT: Well, I see you figured it out. Congrats.

The classloader simply can't find the class input.Main.

The class should be located in the directory ./input , the file inside that directory should be called Main.class and the java command should be ' java input.Main '.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM