简体   繁体   中英

Unable to run compiled Java code - could not find or load main class

I am new to Java, and having problems running my compiled code.

I have a file called AdditionApplication . I compile this line:

javac -cp * AdditionApplication.java

This produces a file in the current directory called AdditionApplication.class

When I try and run the program with the code:

 java -cp * AdditionApplication

It gives the error Error: Could not find or load main class AdditionApplication

What could be a cause of such behavior?

UPDATE

So my code requires a jar file to be included in the initial compilation of the file. This seemed to only work if I had javac - cp * AdditionApplication.java , or if I specified the full path to the jar file.

When I switch over to us a . in 'java -cp . AdditionApplication', I get the error NoClassDefFoundError - which I gather occurs when the class which was initial reference during compilation is no longer available - why would that be the case?

The classpath should be the directory with the files (not * unless they're jar files). Assuming you have a class file (and based on your other command you do), you add that folder to the classpath. Something like,

java -cp . AdditionApplication

Since you have a jar as well, you could do (on *NIX systems)

java -cp .:* AdditionApplication

or on Windows

java -cp .;* AdditionApplication

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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