简体   繁体   中英

NoClassDefFoundError when running a java class through the command prompt

I've just completed quite a large project using IntelliJ and said I'd give the command line statement for people without an IDE to run it (the project will be run on different machines by different people etc). I haven't used the command line in a while and so I'm a bit rusty. I've gotten a NoClassDefFoundError:wrong name and I've been looking at questions on S/O such as Why am I getting a NoClassDefFoundError in Java? but these don't seem to fix my problem. I'm using quite a few external libraries but I have them correctly imported when trying to run. There are many classes compiled but only one will be run so I presume I only run the "java" command on the Main class

java -cp .;poi-3.17/lib/comms-codec-1.10.jar;poi-3.17/lib/commons-collections4-4.1.jar;poi-3.17/lib/commons-logging-1.2.jar;poi-3.17/lib/junit-4.12.jar;poi-3.17/lib/log4j-1.2.17.jar;poi-3.17/ooxml-lib/curvesapi-1.04.jar;poi-3.17/ooxml-lib/xmlbeans-2.6.0.jar;poi-3.17/poi-3.17.jar;poi-3.17/poi-ooxml-3.17.jar bin/com/company/Main

Above is what I've been trying to get working so any suggestions on what I might be doing wrong would be appreciated

The whole exception is "Error: Could not find or load main class Main Caused by: java.lang.NoClassDefFoundError: com/company/Main (wrong name: Main)"

Note, I'm using windows

@caius-brindescu is right, you have to specify the class like com.company.Main but also, you have to execute the class from inside the bin directory. So, run 'cd bin' and then your command and it should work. This is like this because your PATH starts with a dot '.' which means here in this directory .

First, the separator for jar files for the -cp argument is the colon (:), not the semicolon (;). Using the semicolon will change how the arguments are parsed.

Also, when you run the main class, you have to specify the fully qualified name (com.company.Main), instead of the path to the class file. The class file will be resolved from the -cp arguments.

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