问题描述
我很生气不得不问这个问题,但我无法让它工作.目前我有一个项目:
Im quite annoyed at having to ask this but I cant get it to work. Currently I have a project with:
src/文件夹中的 5 个类
2 个名为 profiles.jar 的 JARS 和根目录下的classifier.jar
2 JARS named profiles.jar and classifier.jar in the root folder
我想创建一个makefile?"或批处理文件?"编译和运行这些类从 WINDOWS 命令行,但首先将 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
当我尝试这样做时,它说找不到类,很可能是因为我没有正确地将 jars 添加到构建路径中.在命令提示符中运行它需要使用哪些命令?
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?
谢谢菲利普
编辑
感谢您的帮助,我在让它工作时遇到了很多麻烦目前我有一个项目,src 文件夹中有 5 个类,jar 文件夹中有 2 个 jar
Thanks for the help, Im having alot of trouble getting it to work thoCurrently I have a project with 5 classes in the src folder, and 2 jars in the jar folder
这是命令我正在运行:
设置CLASSPATH=C:wampwwwewsUserProfilingjarsclassifier.jar;C:wampwwwewsUserProfilingjarsprofiles.jar
然后从根文件夹,我运行:
Then from the root folder, Im running:
javac src/*.java
那么:
java -cp ./src:./jars/*源代码/交互
交互是主要的类,我收到了各种 noclassfound 错误,我做错了什么吗?非常感谢菲利普
Interaction is the main class, Im getting all sorts of noclassfound errors, am I doing something wrong?Many thanks Philip
错误
java -cp ./src:./jars/* 交互线程main"中的异常java.lang.NoClassDefFoundError:由以下原因引起的交互作用:java.lang.ClassNotFoundException:相互作用在 java.net.URLClassLoader$1.run(Unknown来源)在 java.security.AccessController.doPrivileged(Native方法)在 java.net.URLClassLoader.findClass(Unknown来源)在 java.lang.ClassLoader.loadClass(Unknown来源)在 sun.misc.Launcher$AppClassLoader.loadClass(Unknown来源)在 java.lang.ClassLoader.loadClass(Unknown来源)在 java.lang.ClassLoader.loadClassInternal(Unknown来源)找不到主类:相互作用.程序将退出.
推荐答案
在早于或等于 Java 版本 5 的版本中,您必须在类路径中单独指定每个 jar,以及源的根目录,例如
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 -cp a.jar:b.jar:c.jar:./src MainClass
在第 6 版中,您可以对 jar 使用通配符,例如
In version 6 you can use wildcards for the jars e.g.
java -cp ./src:* MainClass
但是把你的 jars 放到一个子目录中可能更干净,例如
but it might be cleaner putting your jars into a sub directory e.g.
java -cp ./src:./jars/* MainClass
因此,基本上,您的 makefile 或启动脚本需要构建一个类似于上述命令之一的命令.
So basically, your makefile or start script needs to construct a command like one of the above.
更多信息 - Sun 文档 (v6)
更新 - 为了响应您的第二次编辑,您需要指定完整的主类名称,因此如果该类位于名为com.mypackage.MainClass"的包中,那么您需要执行:
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:
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.
这篇关于在 Windows 命令行中将 Jar 文件添加到 Buildpath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!