无法运行 JAR 文件(在 Eclipse 中创建)

发布于 2024-08-30 14:34:06 字数 1524 浏览 4 评论 0原文

我讨厌以此开始发帖,但我是 Java 新手...我已经关注 本教程创建一个套接字服务器(在 Eclipse 中)。我可以在 Eclipse 中运行服务器,一切都很好。但是当我尝试导出项目时,我不知道如何运行它。我不断收到此错误(它根据我的运行方式而有所不同)

Exception in thread "main" java.lang.NoClassDefFoundError: xsocketserver/Main
Caused by: java.lang.ClassNotFoundException: xsocketserver.Main
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:315)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:330)
at java.lang.ClassLoader.loadClass(ClassLoader.java:250)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:398)

我读过此问题与正在设置的类路径有关。我已经尝试过以下方法:

java -cp libs/xSocket-2.8.12.jar xsocketserver.Main
java -jar xSocketServer.jar    
java -classpath xSocketServer:xSocketServer/libs/xSocket-2.8.12.jar xsocketserver.Main

以及许多其他方法。 JAR 中的文件结构如下:

xSocketServer.jar
    -> xsocketserver
        -> Main.class
        -> xSocketDataHandler.class
    -> META-INF
        -> MANIFEST.MF
    -> libs
        -> xSocket-2.8.12.jar

顺便说一句,我尝试添加自己的清单文件,其中包含 Class-Path 但当我检查它时,它总是显示:

Manifest-Version: 1.0
Main-Class: xsocketserver.Main

我猜这是一个常见的问题基于我在谷歌中看到的点击次数,但我无法理解我做错了什么。也许导出设置错误?

I hate starting a post with this but I'm new to Java... I've followed this tutorial to create a socket server (mines in Eclipse). I can run the server within Eclipse, all is well. But when I try to export the project I can't figure out how to run it. I keep getting this error (it varies depending on how I run it)

Exception in thread "main" java.lang.NoClassDefFoundError: xsocketserver/Main
Caused by: java.lang.ClassNotFoundException: xsocketserver.Main
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:315)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:330)
at java.lang.ClassLoader.loadClass(ClassLoader.java:250)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:398)

I've read this problem relates to class paths being set. I've tried the following:

java -cp libs/xSocket-2.8.12.jar xsocketserver.Main
java -jar xSocketServer.jar    
java -classpath xSocketServer:xSocketServer/libs/xSocket-2.8.12.jar xsocketserver.Main

plus many others.
The file structure within the JAR is as follows:

xSocketServer.jar
    -> xsocketserver
        -> Main.class
        -> xSocketDataHandler.class
    -> META-INF
        -> MANIFEST.MF
    -> libs
        -> xSocket-2.8.12.jar

Incidentally I've tried adding my own manifest file which contains the Class-Path but when I check it it always reads:

Manifest-Version: 1.0
Main-Class: xsocketserver.Main

I'm guessing this is a common problem based on the number of hits I've seen in Google but I can't fathom what I'm doing wrong. Wrong Export settings maybe??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

悲凉≈ 2024-09-06 14:34:06

我不相信 jar 文件可以包含另一个 jar (因为您已经包含了 xSocket-2.8.12.jar)。如果 xSocket 是您自己的代码,请将其直接包含在“外部”jar 中。

查看
http://www.velocityreviews.com/forums/t143595-jars-包含jars.html

类路径在 JAR 中包含 JAR

否则,它对我来说看起来很正确。确保您没有尝试运行该罐子的旧/过时版本。 (删除该 jar 并确保 eclipse 导出一个新的 jar。)

您如何从 eclipse 导出 .jar 文件?生成的 jar 文件通常基于您的运行配置之一。确保您使用的是在 Eclipse 中工作时使用的那个。

除非您已经尝试过,否则请尝试按照下面站点列出的步骤进行操作

http ://forums.sun.com/thread.jspa?threadID=5358121

1-右键单击您的项目并选择导出选项。将弹出导出窗口。

2-从弹出的窗口中选择jar文件选项,该选项将位于窗口的java选项中。

3-单击 JarFile 选项后,将弹​​出一个新窗口。

4-选择 jar 文件的导出目标,然后单击下一步。

5-单击“下一步”后,您将看到一个新屏幕,再次单击“下一步”按钮。

6-现在您将看到一个新屏幕,其中字段名称为“主类”,浏览您的应用程序主类。主类是一次具有 main 方法的类。

7-现在选择完成。

I don't believe a jar-file can include another jar (as you've included xSocket-2.8.12.jar). If xSocket is your own code, let it be included directly in the "outer" jar instead.

Check out
http://www.velocityreviews.com/forums/t143595-jars-containing-jars.html
and
Classpath including JAR within a JAR

Otherwise it looks right to me. Make sure you're not trying to run an old/stale version of the jar. (Delete the jar and make sure eclipse exports a new one.)

How are you exporting the .jar file from eclipse? The generated jar-file will usually be based on one of your run-configurations. Make sure you use the one that you use when it works from eclipse.

Unless you've already tried it, try following the steps (listed at the site below)

http://forums.sun.com/thread.jspa?threadID=5358121

1-Right click on your project and select export option.A export window will get pop-up.

2-Select jar file option from the poped-up window which will be in java option of window.

3-After Clicking JarFile option a new window will get pop-up.

4-Select the export destination for ur jar file and click next.

5-After clicking the next a new screen you will see, click on next button again.

6-Now u will see a new screen which has a field name as "Main Class" browse for your main class of appliaction.main class is once which has main method.

7-Now select finish.

策马西风 2024-09-06 14:34:06

我遇到了类似的问题,您的问题可能相关,也可能不相关...

Jar 清单文件必须采用 UTF-8 格式。我用过windows记事本,但不起作用。当我用另一个编辑器重新创建清单文件时,它工作得很好。

我假设 Eclipse 将创建 UTF-8 文件,但我不确定如果它们不是该格式,它是否会自动转换

如果您确实使用其他编辑器创建清单的第一个版本,请尝试删除 manifest.mf 并重新创建日食内部。

约翰

I had a similar problem and yours may or may not be related...

The Jar manifest file has to be in UTF-8 format. I had used windows notepad and that did not work. When I recreated the manifest file with another editor, it worked fine.

I assume eclipse will create UTF-8 files but I'm not sure if it will automatically convert if they are not in that format

If you did use another editor to create the first version of the manifest, try deleting the manifest.mf and recreating inside of eclipse.

John

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