如何使用此 makefile 以及 src/ 和 bin/ 中的文件正确生成 java jar 文件

发布于 2024-10-09 05:21:41 字数 1421 浏览 3 评论 0原文

我无法为我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

泅人 2024-10-16 05:21:41

我相信您的问题是 bin 前缀。如果您 jar 一个类文件,它必须位于与其完全限定包名称相同的目录中。例如,如果您实现 com.mycompany.Server 类,则 jar 文件中的相应路径必须为 com/mycompany/Server.class。在您的情况下,所有内容都位于 jar 文件的 bin 目录中,因此它找不到您的(可能是非包限定的)类。要么将所有类放入 bin 包中,要么将它们打包成不带 bin 前缀的类。

像这样的事情可能会起作用:

jar: client server
    cd bin; jar cfe ../HBNQServer Server Server.class Job.class JobQueue.class ServerThread.class FileTransfer.class
    cd bin; jar cfe ../HBNQClient Client Client.class Job.class JobQueue.class FileTransfer.class

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 a com.mycompany.Server class, the corresponding path in the jar file must be com/mycompany/Server.class. In your case, everything is in the bin directory in the jar file, so it can't find your (presumably un-package-qualified) classes. Either put all of your classes in the bin package, or jar them up without the bin prefix.

Something like this might work:

jar: client server
    cd bin; jar cfe ../HBNQServer Server Server.class Job.class JobQueue.class ServerThread.class FileTransfer.class
    cd bin; jar cfe ../HBNQClient Client Client.class Job.class JobQueue.class FileTransfer.class
一梦浮鱼 2024-10-16 05:21:41

您需要 JAR 的 清单,如 此处

You need a manifest for your JAR, as discussed here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文