如何使用此 makefile 以及 src/ 和 bin/ 中的文件正确生成 java jar 文件
我无法为我的 Java 实现正确生成 Jar。我不是 Java 大师(这就是为什么我使用 Make 而不是 Ant)。我不认为我关心 Jar,直到我意识到我的 Windows 机器没有 javac。然后我心想“如果我有一个罐子
就好了”所以我在 Makefile 中尝试了几行代码,
相关的行是
all: client server jar
client: bin bin/Job.class bin/JobQueue.class bin/Client.class bin/FileTransfer.class
server: bin bin/Job.class bin/JobQueue.class bin/Server.class bin/ServerThread.class bin/FileTransfer.class
jar: client server
jar cfe HBNQServer Server bin/Server.class bin/Job.class bin/JobQueue.class bin/ServerThread.class bin/FileTransfer.class
jar cfe HBNQClient Client bin/Client.class bin/Job.class bin/JobQueue.class bin/FileTransfer.class
但是当我尝试运行它时我得到了一个 No Class Found 异常。
$ java -jar HBNQServer
Exception in thread "main" java.lang.NoClassDefFoundError: Server
Caused by: java.lang.ClassNotFoundException: Server
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
我确信对于任何更熟悉“Jaring”文件的人来说,这个错误都是非常明显的。
如果我遗漏了任何重要代码,可以在此处找到
I am having trouble correctly generating a Jar for my Java implementation. I am not a Java master ( Thus why I am using Make and not Ant). I didn't think I cared about a Jar until I realized my Windows machine didn't hava javac. Then I thought to myself "If only I had a jar"
So I hacked a few lines together in a Makefile trying
The relevant lines are
all: client server jar
client: bin bin/Job.class bin/JobQueue.class bin/Client.class bin/FileTransfer.class
server: bin bin/Job.class bin/JobQueue.class bin/Server.class bin/ServerThread.class bin/FileTransfer.class
jar: client server
jar cfe HBNQServer Server bin/Server.class bin/Job.class bin/JobQueue.class bin/ServerThread.class bin/FileTransfer.class
jar cfe HBNQClient Client bin/Client.class bin/Job.class bin/JobQueue.class bin/FileTransfer.class
However when I attempt to run it I get a No Class Found Exception.
$ java -jar HBNQServer
Exception in thread "main" java.lang.NoClassDefFoundError: Server
Caused by: java.lang.ClassNotFoundException: Server
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
I am sure the mistake is painfully obvious to anyone more familiar with "Jaring" files.
If I left any important code out, it may be found Here
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我相信您的问题是
bin
前缀。如果您 jar 一个类文件,它必须位于与其完全限定包名称相同的目录中。例如,如果您实现com.mycompany.Server
类,则 jar 文件中的相应路径必须为com/mycompany/Server.class
。在您的情况下,所有内容都位于 jar 文件的bin
目录中,因此它找不到您的(可能是非包限定的)类。要么将所有类放入bin
包中,要么将它们打包成不带bin
前缀的类。像这样的事情可能会起作用:
I believe your problem is the
bin
prefix. If you jar a class file, it must be in a directory identical to its fully qualified package name. For example, if you implement acom.mycompany.Server
class, the corresponding path in the jar file must becom/mycompany/Server.class
. In your case, everything is in thebin
directory in the jar file, so it can't find your (presumably un-package-qualified) classes. Either put all of your classes in thebin
package, or jar them up without thebin
prefix.Something like this might work:
您需要 JAR 的 清单,如 此处。
You need a manifest for your JAR, as discussed here.