Java命令行错误(linux) - 找不到或加载主类

时间:2021-10-21 15:57:59

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

所以我知道这是一个经常发生的熟悉和众所周知的问题。

Most of the time the issue is that the user did not specificy 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)

1-我确保项目的所有java文件都在同一个文件夹中(4个java文件,一个主类)

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

2-我确保所有类都在任何包中

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)

1-使用以下命令编译(风暴需要jar)

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)

2-使用以下命令运行Main类(此处也需要jar,否则我将收到另一个错误)

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)

我仔细检查过HelloStorm是主类的名称(没有拼写错误)

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

我不熟悉从命令行运行框架..所以我可能错过了一些东西,但是搜索类似的问题,阅读oracle文档并尝试不同的方法并没有帮助不幸

What would you be your suggestions?

你有什么建议?

Thanks in advace

谢谢你的推荐

3 个解决方案

#1


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

如果你正确写了这样的主要方法,请检查一下

public class HelloStorm {

    public static void main(String[] args) {

    }

}

#2


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

虽然您希望从命令行执行java程序,但给出以下假设:

  1. class name is HelloStorm
  2. 类名是HelloStorm

  3. package is com.a.b
  4. 包是com.a.b.

  5. the directory in which the compiled class file is present is ~/Public/classes/com/a/b
  6. 编译的类文件所在的目录是〜/ Public / classes / com / a / b

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

要执行该程序,您必须是〜/ Public / classes目录并执行以下命令:

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

#3


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.

如果您在Windows上,则类分隔符为分号。对于Unix,它是冒号。

#1


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

如果你正确写了这样的主要方法,请检查一下

public class HelloStorm {

    public static void main(String[] args) {

    }

}

#2


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

虽然您希望从命令行执行java程序,但给出以下假设:

  1. class name is HelloStorm
  2. 类名是HelloStorm

  3. package is com.a.b
  4. 包是com.a.b.

  5. the directory in which the compiled class file is present is ~/Public/classes/com/a/b
  6. 编译的类文件所在的目录是〜/ Public / classes / com / a / b

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

要执行该程序,您必须是〜/ Public / classes目录并执行以下命令:

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

#3


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.

如果您在Windows上,则类分隔符为分号。对于Unix,它是冒号。