使用多个jar文件和类运行Java程序 [英] Running a java program with multiple jar files and classes
问题描述
我正在使用以下程序编译包含多个jar文件(在lib文件夹内)和类(在src/com文件夹内)的程序.
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是包含main方法的类.如何运行带有多个jar文件和类的Java程序?
okc.java is the class containing the main method. How can I run a java program with multiple jar files and classes?
推荐答案
Java类文件不仅仅是文件本身.表示类包的目录结构是类文件的一部分.您的类路径需要指向最顶层包目录的父目录.
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.
假设您的类是用package com;
声明的,则最顶层的软件包目录是com
.因此,您在类路径中需要com
的 parent :
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
如果您的类不包含任何package
语句,而您刚把它放在com
目录中,则它属于空包,其父目录为com
本身:
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
另一个注意事项:类名及其各自的文件名以大写字母开头是Java约定.原因之一是它使类名易于与程序包组件区分开.
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.
这篇关于使用多个jar文件和类运行Java程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!