java 在 Windows 命令行中将 Jar 文件添加到 Buildpath

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1914493/
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-10-29 18:29:56  来源:igfitidea点击:

Add Jar File to Buildpath in Windows Command Line

javacommand-linemakefilecompilation

提问by Phil

Im quite annoyed at having to ask this but I cant get it to work. Currently I have a project with:

我对不得不问这个很生气,但我无法让它工作。目前我有一个项目:

5 Classes in the src/ folder

2 JARS named profiles.jar and classifier.jar in the root folder

src/ 文件夹中的 5 个类

2 根文件夹中名为profiles.jar 和classifier.jar 的jar

I want to create a "makefile?" or "batch file?" to compile and run these classes FROM THE WINDOWS COMMAND LINE, but first add the jars to the buildpath? Im not sure how I go about this

我想创建一个“makefile?” 或“批处理文件?” 从 WINDOWS 命令​​行编译和运行这些类,但首先将 jar 添加到构建路径?我不知道我是怎么做的

When I try to do it, it says that the class is not found, most likely due to me not adding the jars to the buildpath correctly. What are the commands I need to use to run this in command prompt?

当我尝试这样做时,它说找不到类,很可能是因为我没有正确地将 jars 添加到构建路径中。在命令提示符中运行它需要使用哪些命令?

Thanks Philip

谢谢菲利普

EDIT

编辑

Thanks for the help, Im having alot of trouble getting it to work tho Currently I have a project with 5 classes in the src folder, and 2 jars in the jar folder

感谢您的帮助,我在让它工作时遇到了很多麻烦目前我有一个项目,在 src 文件夹中有 5 个类,在 jar 文件夹中有 2 个 jar

Here are the commands Im running:

这是我运行的命令:

set CLASSPATH=C:\wamp\www\news\UserProfiling\jars\classifier.jar ;C:\wamp\www\news\UserProfiling\jars\profiles.jar

设置 CLASSPATH=C:\wamp\www\news\UserProfiling\jars\classifier.jar ;C:\wamp\www\news\UserProfiling\jars\profiles.jar

Then from the root folder, Im running:

然后从根文件夹,我运行:

javac src/*.java

javac src/*.java

Then:

然后:

java -cp ./src:./jars/* src/Interaction

java -cp ./src:./jars/* src/交互

Interaction is the main class, Im getting all sorts of noclassfound errors, am I doing something wrong? Many thanks Philip

交互是主要类,我收到了各种 noclassfound 错误,我做错了什么吗?非常感谢菲利普

THE ERROR

错误

java -cp ./src:./jars/* Interaction Exception in thread "main" java.lang.NoClassDefFoundError: Interaction Caused by: java.lang.ClassNotFoundException: Interaction at java.net.URLClassLoader$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClassInternal(Unknown Source) Could not find the main class: Interaction. Program will exit.

java -cp ./src:./jars/* 线程“main”中的交互异常 java.lang.NoClassDefFoundError: Interaction Caused by: java.lang.ClassNotFoundException: Interaction at java.net.URLClassLoader$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClassInternal(Unknown Source) 找不到主类:Interaction。程序将会退出。

回答by Joel

In version older than or equal to Java version 5 you must specify each jar individually, and the root of your source, on your classpath e.g.

在早于或等于 Java 5 的版本中,您必须在类路径上单独指定每个 jar 和源的根目录,例如

java -cp a.jar:b.jar:c.jar:./src MainClass

In version 6 you can use wildcards for the jars e.g.

在第 6 版中,您可以为罐子使用通配符,例如

java -cp ./src:* MainClass

but it might be cleaner putting your jars into a sub directory e.g.

但将罐子放入子目录可能更干净,例如

java -cp ./src:./jars/* MainClass

So basically, your makefile or start script needs to construct a command like one of the above.

所以基本上,你的 makefile 或启动脚本需要构造一个类似于上述之一的命令。

More info- Sun docs (v6)

更多信息- Sun 文档 (v6)

Update- in response to your second edit, you need to specify the full main class name, so if the class is in a package called 'com.mypackage.MainClass' then you need to do:

更新- 为了响应您的第二次编辑,您需要指定完整的主类名称,因此如果该类位于名为“com.mypackage.MainClass”的包中,那么您需要执行以下操作:

java -cp ./src:./jars/* com.mypackage.MainClass

I'd also suggest getting the command working as a standalone command first, before getting the whole script running. By removing moving parts it will be faster to debug and easier to see what's going on.

我还建议先将命令作为独立命令运行,然后再运行整个脚本。通过移除移动部件,调试速度会更快,并且更容易看到正在发生的事情。

回答by Thomas L?tzer

I would suggest you take a look at antor maven. Ant is a solution to do pretty much straightforward what you want to do, maven is not as straightforward but has its advantages when it comes to managing dependencies.

我建议你看看antmaven。Ant 是一个解决方案,可以非常简单地完成您想做的事情,maven 没有那么简单,但在管理依赖项方面有其优势。

回答by Valentin Rocher

About your second question : if you use

关于你的第二个问题:如果你使用

java -cp ./src:./jars/* src/Interaction

it will try to launch the class src/Interaction, which does not exists. src is already in your classpath, so you just have to do

它将尝试启动不存在的类 src/Interaction。src 已经在你的类路径中,所以你只需要做

java -cp ./src:./jars/* Interaction

回答by Carl Smotricz

Found your problem, I think.

发现你的问题,我想。

javac src/*.java

java -cp ./src:./jars/* src/Interaction

javac src/*.java

java -cp ./src:./jars/* src/交互

This problem plagues many beginners: You need to run javac and java from the directory where your source tree starts. In your case, that directory is src. Since srcdoes not appear in your package names, it should also not appear in your compile/execution paths.

这个问题困扰着很多初学者:你需要从你的源代码树开始的目录中运行 javac 和 java。在您的情况下,该目录是src. 由于src没有出现在你的包名中,它也不应该出现在你的编译/执行路径中。

So you should

所以你应该

cd UserProfiling/src 

and run

并运行

javac *.java 

and

java -cp .:../jars Interaction 

from there.

从那里。