Java 设置 JAR 文件的类路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9823729/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 08:08:34  来源:igfitidea点击:

Setting the classpath for JAR files

javamysqlbatch-filejar

提问by JD87

I have recently just created Java project using Eclipse that requires 2 JAR files (phiget21.jar and the mysql.jar)

我最近刚刚使用 Eclipse 创建了需要 2 个 JAR 文件(phiget21.jar 和 mysql.jar)的 Java 项目

Everything works fine when running the programme in Eclipse, and I have noticed the the jar files are saved in a 'lib' folder.

在 Eclipse 中运行程序时一切正常,我注意到 jar 文件保存在“lib”文件夹中。

I soon going to me moving the programme off my computer to be used on other machines, so I decided to create a batch file to compile all of the classes and then run.

我很快就会将程序从我的计算机上移出以在其他机器上使用,因此我决定创建一个批处理文件来编译所有类然后运行。

However, I am having trouble with the locating of the jar files. In the batch file do I require a command something like: set classpath=.:..;mysql.jar:../phidget21.jar, before the compilation of the Java classes?

但是,我在定位 jar 文件时遇到了问题。在批处理文件中set classpath=.:..;mysql.jar:../phidget21.jar,在编译 Java 类之前,我是否需要类似以下内容的命令?

I have read that the dots (...) have something to do with directories but not entirely sure how to implement them.

我读过点 (...) 与目录有关,但不完全确定如何实现它们。

My programme is currently saved in these locations:

我的程序目前保存在以下位置:

Project/src/.java files (I have also put the .jar files in here as well as i thought this may make thing s easier)

项目/src/.java 文件(我也把 .jar 文件放在这里,我认为这可能会让事情变得更容易)

Project/lib/ .jar files

项目/lib/.jar 文件

Any help would be greatly appreciated!

任何帮助将不胜感激!

采纳答案by Chandra Sekhar

while setting the classpath a single dot (.) means current directory. As you jar files are in current directory, you just need to go to your current directory using cd command in DOS prompt, then use

设置类路径时,单点 (.) 表示当前目录。由于您的 jar 文件位于当前目录中,因此您只需要在 DOS 提示符下使用 cd 命令转到当前目录,然后使用

set classpath = .;filename.jar;another filename.jar

Here . represents current directory and semicolon separates each classpaths.

这里 。代表当前目录,分号分隔每个类路径。

You can even set classpath of more than one jar files using wild card character * which can be read as all.

您甚至可以使用通配符 * 设置多个 jar 文件的类路径,该通配符可以读取为所有.jar 文件。

回答by krystan honour

You need something like

你需要类似的东西

java -classpath lib/foo.jar:. com.company.Program

you can also use wildcards since java 6. see here

从 java 6 开始,您还可以使用通配符。请参见此处

so the above becomes

所以上面变成

java -classpath lib/*:. com.company.Program