问题
I am compiling a program with multiple jar files (inside the lib folder) and classes (inside the src/com folder) with:
javac -classpath lib/\* src/com/*.java
I typed this to run the program:
java -cp lib/\* src/com/okc
But it doesn't work. Instead, I get this:
Error: Could not find or load main class src.com.okc
okc.java is the class containing the main method. How can I run a java program with multiple jar files and classes?
回答1:
A Java class file is not just the file itself. The directory structure which represents a class's package is part of the class file. Your classpath needs to point to the directory which is the parent of the topmost package directory.
Assuming your class is declared with package com;
, the topmost package directory is com
. So you need the parent of com
in your classpath:
java -classpath src:lib/\* com.okc
If your class does not contain any package
statement, and you just happened to put it in a com
directory, then it belongs to the null package, whose parent directory is com
itself:
java -classpath src/com:lib/\* okc
An additional note: It is Java convention to have class names, and their respective file names, start with an uppercase letter. One reason is that it makes class names easy to distinguish from package components.
回答2:
Try:
java -cp ../lib/\* com.okc
from the src directory (not sure...)
回答3:
Assuming that your current directory has your lib/ :
java -cp lib src.com.okc
来源:https://stackoverflow.com/questions/30368595/running-a-java-program-with-multiple-jar-files-and-classes