views:

408

answers:

4

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.
+2  A: 

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.

cletus
And run from that directory that contains the `input` directory rather than the `input` directory itself (which should contain `Main.class`).
Tom Hawtin - tackline
A: 

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.

rtperson
A: 

Try CDing to the parent directory, and running javac (and then java) from there. E.g.,

javac parent/Main.java
java parent/Main
Matt Ball
A: 

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'.

Andreas_D