1

I am using java in ubuntu machine. I can run java program from my eclipse. But I have some work that that I have to perform from command prompt. I could compile java program using javac HelloWorld.java command and its create the HelloWorld.class in same directory. But when I run java HelloWorld command its gives me following error.

Error: Could not find or load main class

I dont know why???

can anyone tell me what's the problem?

1

Check you CLASSPATH. It should contain current directory (aka ".").

  • when I echo $CLASSPATH it shows the following path /opt/lucene-3.5.0/lucene-core-3.5.0.jar:/opt/lucene-3.5.0/contrib/demo/lucene-demo-3.5.0.jar:/opt/semanticvectors/semanticvectors-3.2.jar and when i echo $JAVA_HOME it shows /usr/lib/jvm/java-7-oracle – tetrapack Jan 15 '14 at 10:27
  • Then add '.' to your $CLASSPATH with CLASSPATH=.:$CLASSPATH or as a paramater with java -classpath .:$CLASSPATH HelloWorld – ragol Jan 17 '14 at 10:36
0

Did you correctly create a main method in your java file, did you name the java file exactly like the class?

$ cat HelloWorld.java

public class HelloWorld {

        public static void main(String[] args) {

                System.out.println("Bonjour le monde !");

        }

}

$ javac HelloWorld.java
$ java HelloWorld
Bonjour le monde !
0

I was executing

java HelloWorld.class

instead of

java HelloWorld

Problem Solved!

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.