-1

I know that this a familiar and well-known issue that happens a lot.

Most of the time the issue is that the user did not specify the package if the class is in package. However I made sure that my case is different.

I tried the following

1- I made sure that the all the java files of the project are in the same folder (4 java files one with main class)

2- I made sure that non of the classes are in any packages

Then I tried to run the program as following (It is a storm hello-world example in case you are familiar with it)

1- Compile using the following command (The jar is needed for storm)

javac -classpath ~/Public/apache-storm-0.9.4/lib/storm-core-0.9.4.jar *.java

2- Run Main class using the following command (The jar is also needed here otherwise I'll get another error)

java -cp ~/Public/apache-storm-0.9.4/lib/storm-core-0.9.4.jar HelloStorm

What I get is the error below

Error: Could not find or load main class HelloStorm

I double checked that HelloStorm is the name of the main class (with no spelling mistakes)

I am not familiar with running framework from command line .. so I might have missed something, however searching similar problem, reading oracle documentation and trying different approaches did not help unfortunately

What would you be your suggestions?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Abdelrahman Shoman
  • 2,882
  • 7
  • 36
  • 61
  • May be this detailed solution might work http://stackoverflow.com/questions/18093928/what-does-could-not-find-or-load-main-class-mean – Geek May 04 '15 at 10:22
  • try running java -cp "~/Public/apache-storm-0.9.4/lib/storm-core-0.9.4.jar:." HelloStorm Also, if HelloStorm is in a package say com.a.b, you will have to run it like: java -cp "~/Public/apache-storm-0.9.4/lib/storm-core-0.9.4.jar:." com.a.b.HelloStorm – Anshuman May 04 '15 at 10:26
  • which is the class with the `main` mehod? – Blip May 04 '15 at 10:27
  • @Anshuman when I do that .. it seems that the jar file is not loaded which cause an error – Abdelrahman Shoman May 04 '15 at 10:34
  • @Blip public class HelloStorm – Abdelrahman Shoman May 04 '15 at 10:34
  • 1
    are you sure 'HelloStorm.class' exists in the path from where you are issuing the `java` command? – Blip May 04 '15 at 10:41
  • @Blip Yes, all the classes are under the same repository that has "HelloStorm.class" .. I also double checked my location in the terminal and issued the "ls" command to make sure I am in the right place .. however still the same problem – Abdelrahman Shoman May 04 '15 at 10:50
  • 1
    update your question with the package details of HelloStorm and the complete path from where you are using the java command – Anshuman May 05 '15 at 05:17

3 Answers3

0

please check that if you have correctly written the main method like this

public class HelloStorm {

    public static void main(String[] args) {

    }

}
Blip
  • 3,061
  • 5
  • 22
  • 50
blueDexter
  • 977
  • 11
  • 14
  • I don't think throwing an exception might cause any issue, right ? ... "public static void main(String[] args) throws Exception{ } " – Abdelrahman Shoman May 04 '15 at 10:31
  • No it should not be a problem. But this is a crude of way writing code. However, once I had faced issue with some situation. i cant remember the exact situation. But my suggestion is dont make this a practice. – blueDexter May 04 '15 at 10:41
  • just check that main method is exactly like this public static void main(String[] args) { } – blueDexter May 04 '15 at 10:42
  • Throwing an exception that way may not be the best practice but it is just a helloWorld example that I did not write my self ... Anyway I have double checked the main method signature, still same problem – Abdelrahman Shoman May 04 '15 at 10:48
  • test it without attaching the jar. just empty main method or with just System.out.println("Hello world statement"); – blueDexter May 04 '15 at 10:52
0

You compile to classes to your current location but you don't use it when you run program. Add it and it will run fine

java -cp .:~/Public/apache-storm-0.9.4/lib/storm-core-0.9.4.jar HelloStorm

If you are on Windows then class separator is semicolon. For Unix it is colon.

Marsha
  • 187
  • 1
  • 8
  • I received the following error "java.lang.NoClassDefFoundError: backtype/storm/topology/IRichSpout" ..... The error mean that the jar file was not read or loaded – Abdelrahman Shoman May 04 '15 at 10:30
  • If you are on Windows then class sepator is semicolon. For Unix it is colon. Also old versions of Java do not support -cp. Use full -classpth switch. – Marsha May 04 '15 at 20:55
0

While you want to execute a java program from command line given the following assumptions:

  1. class name is HelloStorm
  2. package is com.a.b
  3. the directory in which the compiled class file is present is ~/Public/classes/com/a/b

To execute the program, you will have to be ~/Public/classes directory and execute the following command:

 java -cp ~/Public/apache-storm-0.9.4/lib/storm-core-0.9.4.jar com.a.b.HelloStorm
Anshuman
  • 831
  • 5
  • 18