简体   繁体   English

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

[英]Unable to run JAR file (created in Eclipse)

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). 我讨厌以此为起点,但是我是Java的新手...我已经按照本教程创建了套接字服务器(Eclipse中的地雷)。 I can run the server within Eclipse, all is well. 我可以在Eclipse中运行服务器,一切都很好。 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: JAR中的文件结构如下:

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: 顺便说一句,我尝试添加自己的清单文件,其中包含Class-Path但是当我检查该清单文件时,它始终显示为:

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. 根据我在Google中看到的点击数,我猜这是一个普遍的问题,但我无法理解我做错了什么。 Wrong Export settings maybe?? 可能是错误的导出设置?

I don't believe a jar-file can include another jar (as you've included xSocket-2.8.12.jar). 我不相信一个jar文件可以包含另一个jar(因为您已经包含了xSocket-2.8.12.jar)。 If xSocket is your own code, let it be included directly in the "outer" jar instead. 如果xSocket是您自己的代码,则应将其直接包含在“外部” jar中。

Check out http://www.velocityreviews.com/forums/t143595-jars-containing-jars.html and Classpath including JAR within a JAR 查看http://www.velocityreviews.com/forums/t143595-jars- contains-jars.htmlClasspath,包括JAR中的JAR

Otherwise it looks right to me. 否则对我来说看起来不错。 Make sure you're not trying to run an old/stale version of the jar. 确保您不尝试运行旧版本的jar。 (Delete the jar and make sure eclipse exports a new one.) (删除罐子,并确保eclipse导出一个新罐子。)

How are you exporting the .jar file from eclipse? 您如何从Eclipse导出.jar文件? The generated jar-file will usually be based on one of your run-configurations. 生成的jar文件通常将基于您的一种运行配置。 Make sure you use the one that you use when it works from eclipse. 确保使用从eclipse运行时使用的那个。

Unless you've already tried it, try following the steps (listed at the site below) 除非您已经尝试过,否则请尝试执行以下步骤(在下面的网站上列出)

http://forums.sun.com/thread.jspa?threadID=5358121 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. 1-右键单击您的项目,然后选择导出选项。将弹出一个导出窗口。

2-Select jar file option from the poped-up window which will be in java option of window. 2-从弹出的窗口中选择jar文件选项,该选项位于window的java选项中。

3-After Clicking JarFile option a new window will get pop-up. 3-单击JarFile选项后,将弹​​出一个新窗口。

4-Select the export destination for ur jar file and click next. 4-选择您的jar文件的导出目标,然后单击下一步。

5-After clicking the next a new screen you will see, click on next button again. 5-单击下一个新屏幕后,您将看到,再次单击下一个按钮。

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. 6-现在,您将看到一个新屏幕,该屏幕的字段名称为“ Main Class”(主类),浏览您的appliaction主类。main class曾经具有main方法。

7-Now select finish. 7-现在选择完成。

I had a similar problem and yours may or may not be related... 我有类似的问题,您的关系可能会或可能不会...

The Jar manifest file has to be in UTF-8 format. Jar清单文件必须为UTF-8格式。 I had used windows notepad and that did not work. 我曾经用过Windows记事本,但没有用。 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 我认为eclipse将创建UTF-8文件,但是如果它们不是该格式,我不确定它是否会自动转换。

If you did use another editor to create the first version of the manifest, try deleting the manifest.mf and recreating inside of eclipse. 如果确实使用其他编辑器创建清单的第一个版本,请尝试删除manifest.mf并在eclipse内部重新创建。

John 约翰

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM