为什么我的.jar文件引发异常,即使我的程序在Eclipse中没有异常运行? [英] Why does my .jar file raise exceptions even if my program runs without exceptions in Eclipse?

查看:245
本文介绍了为什么我的.jar文件引发异常,即使我的程序在Eclipse中没有异常运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Java的新手,学习如何将应用程序导出到.jar文件。我根据 Eclipse和Java for Total Beginners的教程,在Eclipse中编写了一个简单的Java应用程序, a>。

I am new to Java and learning how to export applications to .jar files. I made a simple Java application in Eclipse with a few classes based on the tutorial from Eclipse and Java for Total Beginners.

当我通过运行 - >在Eclipse中运行我的应用程序时,我的应用程序运行没有例外。但是,当我将文件 - >导出并将我的应用程序导出并导出到.jar文件中,然后执行

When I run my application via Run -> Run in Eclipse, my application runs without exceptions. However, when I got to File -> Export and export my application into a .jar file and then execute

java myLibrary.jar

Exception in thread "main" java.lang.NoClassDefFoundError: myLibrary/jar

Caused by: java.lang.ClassNotFoundException: myLibrary.jar
    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:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)

这是什么意思?我怎么缺课?我尝试检查并取消选中要导出的项目。例如,我尝试包括我的JUnit测试无效。

What does this mean? How am I missing classes? I tried checking and unchecking items to export. For instance, I tried including my JUnit tests to no avail.

推荐答案

在终端中,转到包含所有类的目录文件。

In the terminal go to the directory that contains all your class files.

jar cmf MANIFEST.MF myLibrary.jar *

然后,如果jar创建成功,您可以通过以下方式运行:

Then if the jar is created successfully you can run it by:

java -jar myLibrary.jar

MANIFEST.MF文件应至少包含:

The MANIFEST.MF file should contain at the very least:

Main-Class: myLibrary

其中myLibrary是包含您的主要功能的类。

Where myLibrary is the class that contains your main function.

这篇关于为什么我的.jar文件引发异常,即使我的程序在Eclipse中没有异常运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆