我很不高兴不得不问这个,但我不能让它工作.目前我有一个项目:
5 Classes in the src/ folder
2 JARS named profiles.jar and
classifier.jar in the root folder
我想创建一个“makefile?”还是“批处理文件?”从WINDOWS命令行编译和运行这些类,但首先将jar添加到buildpath?我不知道我是怎么回事
当我尝试这样做时,它表示找不到类,很可能是因为我没有正确地将jar添加到构建路径中.在命令提示符下运行此命令需要使用哪些命令?
谢谢
菲利普
编辑
感谢您的帮助,我有很多麻烦让它工作
目前我有一个项目,在src文件夹中有5个类,在jar文件夹中有2个jar
这是命令
我在跑:
set
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/* 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版本5的版本中,您必须在类路径上单独指定每个jar以及源的根目录,例如
java -cp a.jar:b.jar:c.jar:./src MainClass
在版本6中,您可以使用通配符作为jar,例如
java -cp ./src:* MainClass
但将你的罐放入子目录可能更清洁,例如
java -cp ./src:./jars/* MainClass
所以基本上,你的makefile或者启动脚本需要构建一个像上面那样的命令.
更新 – 为了响应您的第二次编辑,您需要指定完整的主类名,因此如果该类位于名为“com.mypackage.MainClass”的包中,那么您需要执行以下操作:
java -cp ./src:./jars/* com.mypackage.MainClass
我还建议在使整个脚本运行之前,先将命令作为独立命令运行.通过移除移动部件,调试速度更快,更容易看到发生了什么.
标签:java,command-line,compilation,makefile
来源: https://codeday.me/bug/20190527/1159829.html