Bob Dylan
Bob Dylan

Reputation: 4453

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.

Upvotes: 0

Views: 1971

Answers (4)

venkatesh
venkatesh

Reputation: 19

Open terminal and paste this command:

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

Upvotes: 1

Andreas Dolk
Andreas Dolk

Reputation: 114787

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

Upvotes: 0

rtperson
rtperson

Reputation: 11708

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.

Upvotes: 0

cletus
cletus

Reputation: 625097

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.

Upvotes: 3

Related Questions