简体   繁体   English

设置 JAR 文件的类路径

[英]Setting the classpath for JAR files

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.但是,我在定位 jar 文件时遇到了问题。 In the batch file do I require a command something like: set classpath=.:..;mysql.jar:../phidget21.jar , before the compilation of the Java classes?在批处理文件中,在编译 Java 类之前,我是否需要一个类似于: set classpath=.:..;mysql.jar:../phidget21.jar的命令?

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!任何帮助将不胜感激!

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 文件。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM