在 Windows 命令行中将 Jar 文件添加到构建路径
我很恼火不得不问这个问题,但我无法让它发挥作用。目前我有一个项目:
src/文件夹中有5个类
2 个名为 profile.jar 的 JAR 和 根文件夹中的classifier.jar
我想创建一个“makefile?”或“批处理文件?” 从 WINDOWS 命令行编译并运行这些类,但首先将 jar 添加到构建路径?我不确定如何解决这个问题
当我尝试这样做时,它说找不到该类,很可能是因为我没有将 jars 正确添加到构建路径。我需要使用哪些命令才能在命令提示符下运行它?
谢谢 菲利普
编辑
感谢您的帮助,我在让它工作时遇到了很多麻烦 目前我有一个项目,src 文件夹中有 5 个类,jar 文件夹中有 2 个 jar
以下是命令 我正在跑步:
设置 CLASSPATH=C:\wamp\www\news\UserProfiling\jars\classifier.jar ;C:\wamp\www\news\UserProfiling\jars\profiles.jar
然后从根文件夹中运行:
javac src/*.java
然后:
java -cp ./src:./jars/* src/交互
Interaction 是主类,我收到各种 noclassfound 错误,我做错了什么吗? 非常感谢菲利普
错误
java -cp ./src:./jars/* 交互 线程“main”中的异常 java.lang.NoClassDefFoundError: 交互作用产生的原因: java.lang.ClassNotFoundException: 相互作用 在 java.net.URLClassLoader$1.run(未知 来源) 在java.security.AccessController.doPrivileged(本机 方法) 在 java.net.URLClassLoader.findClass(未知 来源) 在java.lang.ClassLoader.loadClass(未知 来源) 在 sun.misc.Launcher$AppClassLoader.loadClass(未知 来源) 在java.lang.ClassLoader.loadClass(未知 来源) 在java.lang.ClassLoader.loadClassInternal(未知 来源)找不到主类: 相互作用。程序将退出。
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
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
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?
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
Here are the commands
Im running:
set
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
Then:
java -cp ./src:./jars/*
src/Interaction
Interaction is the main class, Im getting all sorts of noclassfound errors, am I doing something wrong?
Many thanks Philip
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在早于或等于 Java 版本 5 的版本中,您必须在类路径上单独指定每个 jar 以及源的根目录,例如
在版本 6 中,您可以对 jar 使用通配符,例如,
但将 jar 放入子目录中可能会更干净目录 例如
所以基本上,您的 makefile 或启动脚本需要构造一个类似于上述命令之一的命令。
更多信息 - Sun 文档(v6)
更新 - 为了响应您的第二次编辑,您需要指定完整的主类名称,因此如果该类位于名为“com.mypackage.MainClass”的包中,则你需要这样做:
我还建议先让该命令作为独立命令运行,然后再运行整个脚本。通过移除移动部件,调试速度会更快,并且更容易查看正在发生的情况。
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.
In version 6 you can use wildcards for the jars e.g.
but it might be cleaner putting your jars into a sub directory e.g.
So basically, your makefile or start script needs to construct a command like one of the above.
More info - Sun docs (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:
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.
我建议您查看 ant 或 maven. Ant 是一个可以非常简单地完成您想做的事情的解决方案,maven 并不那么简单,但在管理依赖项方面有其优势。
I would suggest you take a look at ant or 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.
关于你的第二个问题:如果你使用
它,它将尝试启动类 src/Interaction,该类不存在。 src 已经在你的类路径中,所以你只需要做
About your second question : if you use
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
我想找到你的问题了。
这个问题困扰着许多初学者:您需要从源代码树开始的目录运行 javac 和 java。在您的例子中,该目录是 src。由于 src 不会出现在您的包名称中,因此它也不应该出现在您的编译/执行路径中。
所以你应该
逃跑。
从
那里
Found your problem, I think.
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
. Sincesrc
does not appear in your package names, it should also not appear in your compile/execution paths.So you should
and run
and
from there.