简体   繁体   English

Java“无法找到或加载主类”,类路径错误

[英]Java “could not find or load main class” with classpath error

I have been facing an issue while compiling with -cp or the -classpath flag in Java. 在Java中使用-cp或-classpath标志进行编译时,我遇到了一个问题。 I will try to explain the problem below: 我将尝试解释下面的问题:

Let's say; 我们说吧; I have two files - A.java and B.java. 我有两个文件 - A.java和B.java。 A.java has a simple public class with a private instance variable, one get and one set method. A.java有一个简单的公共类,它有一个私有实例变量,一个get和一个set方法。 B.java is the driver method for A. It instantiates A; B.java是A的驱动方法。它实例化A; sets some value for A's instance variable and finally prints out the value using the get method in A. 为A的实例变量设置一些值,最后使用A中的get方法打印出该值。

I can compile both A.java and B.java from command line. 我可以从命令行编译A.java和B.java。 If both compiled class files are in same folder; 如果两个编译的类文件都在同一个文件夹中; the following runs fine: 以下运行正常:

java B

However; 然而; say I keep A.class in a separate folder. 说我把A.class保存在一个单独的文件夹中。 Or even better; 甚至更好; I make a JAR file of the class file A.class. 我创建了一个类文件A.class的JAR文件。 Now; 现在; I should be able to compile B.java with the classpath properly stated. 我应该能够使用正确陈述的类路径编译B.java。

The compilation works. 编译工作。

javac -cp ..\\Lib\\A.jar -d ..\\Bin B.java

It puts the B.class file in Bin folder as expected. 它按预期将B.class文件放在Bin文件夹中。 Now; 现在; if I go to Bin folder and try to execute the following; 如果我去Bin文件夹并尝试执行以下操作;

java -cp ..\Lib\A.jar B

I get the error: 我收到错误:

Error: Could not find or load main class B

Now; 现在; I have been trying in vain to solve this problem for past few days. 过去几天我一直试图解决这个问题。 This is a simple use case to demonstrate the problem; 这是一个简单的用例来演示这个问题; but in reality - I am not being able to link to an existing JAR library using -classpath or -cp flag. 但实际上 - 我无法使用-classpath或-cp标志链接到现有的JAR库。 The only way I can run my Java programs from command line is if I extract the class files from JAR archive in the same directory as the output class. 我可以从命令行运行Java程序的唯一方法是从JAR存档中提取类文件与输出类在同一目录中。 So, then I would not need to include the classpath flag in the execution command. 那么,我就不需要在执行命令中包含classpath标志。

But this is something I don't want. 但这是我不想要的。 I want to keep my JAR archive separate from the output class files of my source code. 我想将我的JAR存档与源代码的输出类文件分开。 I know that using an IDE; 我知道使用IDE; this is something I won't need to worry about. 这是我不需要担心的事情。 But I am more interested in the command line solution; 但我对命令行解决方案更感兴趣; to understand what goes behind the hood. 了解引擎盖后面的内容。

I have gone through all other suggestions on StackOverflow regarding this - but none seems to work. 我已经对StackOverflow上的所有其他建议进行了讨论 - 但似乎没有任何建议。 FYI, I am using default package in all cases. 仅供参考,我在所有情况下都使用默认包。 No packaging is explicitly specified. 没有明确指定包装。

Please help. 请帮忙。 Thanks in advance! 提前致谢!

Update: I have been asked to provide my directory structure in a readable format, so here goes: 更新:我被要求以可读格式提供我的目录结构,所以这里是:

$pwd
C:\My\Path\To\Java\Programs\Top
$ls
Source
Bin
Lib
$cd Bin 
$ls
B.class
$ls ..\Source\
A.java
B.java
$ls ..\Lib
A.jar
$jar tf ..\Lib\A.jar
META-INF
META-INF/MANIFEST.MF
A.java
A.class

I hope that clears it. 我希望能够清除它。 I am using Windows Powershell, so sub-directories are marked with "\\". 我使用的是Windows Powershell,因此子目录标有“\\”。 If I were typing in Unix terminal, it would have been "/" instead. 如果我在Unix终端中输入,那么它将是“/”。 By the way, I tried this on Ubuntu 14.04, with no avail. 顺便说一句,我在Ubuntu 14.04上试过这个,但没有用。 I have also tested this with jdk 1.6 to jdk 1.8 - again, same error. 我也用jdk 1.6到jdk 1.8进行了测试 - 同样的错误。

You are missing out the "classpath" while trying to run your program. 在尝试运行程序时,您错过了“类路径”。

One is mistaken to think that "-cp ..\\Lib\\A.jar" is to include A.class during the runtime. 有人误以为“-cp .. \\ Lib \\ A.jar”是在运行时包含A.class。 But the thing is that, "-cp" option is to set the classpath and not include. 但问题是,“-cp”选项是设置类路径而不包括。 That way one is missing the classpath for the class "B". 这样就缺少了类“B”的类路径。

So to run your program, from the "Bin" folder, the correct command (for any Windows system) is : "java -cp .;..\\Lib\\A.jar B". 因此,要运行程序,从“Bin”文件夹,正确的命令(对于任何Windows系统)是:“java -cp。; .. \\ Lib \\ A.jar B”。 The "." “。” makes all the difference, referring to the current directory. 使用当前目录,完全不同。

For Linux, the command, from the "Bin" folder, becomes : "java -cp .:../Lib/A.jar B". 对于Linux,来自“Bin”文件夹的命令变为:“java -cp。:../ Lib / A.jar B”。

The main difference is of the separator, " ; " is for Windows and " : " is for Linux. 主要区别在于分隔符,“ ; ”用于Windows,“ ”用于Linux。

instead of the following: 而不是以下:

java -cp ..\\Lib\\A.jar B java -cp .. \\ Lib \\ A.jar B.

provide this 提供这个

java B -cp ..\\Lib\\A.jar java B -cp .. \\ Lib \\ A.jar

Thanks, 谢谢,

Aritra Aritra

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM