0

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.

4 Answers 4

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

And run from that directory that contains the input directory rather than the input directory itself (which should contain Main.class).
1

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

1 Comment

This has nothing to do with tomcat.
0

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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.