Java "could not find or load main class" with classpath error

I had a problem compiling with -cp or the -classpath flag in Java. I will try to explain the problem below:

Let's say; I have two files - A.java and B.java. A.java has a simple public class with a private instance variable, one retrieval method, and one set. B.java is the driver method for A. It creates an instance of A; sets some value for the instance variable A and finally outputs the value using the get method in A.

I can compile both A.java and B.java from the command line. If both compiled class files are in the same folder; following fines:

java B

Nonetheless; let's say I keep A.class in a separate folder. Or better yet; I am making a JAR file for class file A.class. Now; I would have to compile B.java with the appropriate class specified.

Compilation work.

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

It places the B.class file in the Bin folder as expected. Now; if I go to the Bin folder and try the following;

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

I get an error message:

 Error: Could not find or load main class B 

Now; I have tried in vain to solve this problem in the last few days. This is a simple example to demonstrate the problem; but actually - I cannot reference the existing JAR library using -classpath or -cp. The only way to run my Java programs from the command line is to extract the class files from the JAR archive in the same directory as the output class. So then I will not need to include the classpath flag in the execution command.

But that I do not want. I want to keep my JAR archive separate from the output class files of my source code. I know using an IDE; this is something that I will not worry about. But I'm more interested in a command line solution; to understand what goes behind the hood.

I reviewed all the other StackOverflow suggestions regarding this, but no one seems to work. FYI, I use the default package in all cases. An explicit specification is not specified.

Please, help. Thanks in advance!

Update: I was asked to provide my directory structure in a readable format, so that:

 $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 this clears. I use Windows Powershell, so the subdirectories are marked with "\". If I typed in a Unix terminal, that would be "/". By the way, I tried this on Ubuntu 14.04, to no avail. I also tested this with jdk 1.6 on jdk 1.8 - again, with the same error.

+6
source share
2 answers

When you try to run your program, you skip the "class path".

One mistakenly believes that "-cp .. \ Lib \ A.jar" should include A.class at run time. But the fact is that the -cp option is to set the class path and not include it. Thus, there is no classpath class for class "B".

So, to run your program, the correct command (for any Windows system) from the "Bin" folder is: "java -cp.; .. \ Lib \ A.jar B". "." does its best referring to the current directory.

For Linux, the command from the "Bin" folder becomes: "java -cp.:../Lib/A.jar B".

The main difference between the separator: " ; " for Windows and " : " for Linux.

+5
source

instead of the following:

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

indicate this

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

Thanks,

Aritra

-1
source

Source: https://habr.com/ru/post/972070/


All Articles