Running a java program with multiple jar files and classes [duplicate]

旧巷老猫 提交于 2019-12-12 10:18:42

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!