Jar文件给出异常

时间:2013-04-14 16:33:11

标签: jar jvm java executable-jar

我创建了在cmd上成功运行的jar文件,如下所示:

  

启动javaw -Xmx1024m -jar JavaEdit.jar

我的应用程序通过双击运行,但是当我执行一个按钮时,它会给出异常,当我通过CMD运行时,它不会发生。

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at Editor.compileIt(Editor.java:2407)
    at Editor.CompileBtnActionPerformed(Editor.java:1412)
    at Editor.access$300(Editor.java:113)
    at Editor$7.actionPerformed(Editor.java:580)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$200(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

为什么会这样,请帮忙......谢谢

源代码中的第2407行:

    StandardJavaFileManager fileManager = compiler.getStandardFileManager(c,
            Locale.ENGLISH,
            null);

2 个答案:

答案 0 :(得分:3)

如果你得到一个null JavaCompiler(或NPE得到它),那么我猜你用JRE而不是JDK运行你的应用程序。

来自JavaDoc

  

返回:此平台提供的编译器;如果未提供编译器,则返回null

答案 1 :(得分:3)

NullPointerException唯一可能的位置是这一行:

StandardJavaFileManager fileManager = compiler.getStandardFileManager(c,

compilernull。您应该添加println(System.getPropert("java.home"))语句以查看正在运行应用程序的JRE / JDK。

我的猜测是你的JAVA_HOME指向JDK且位于PATH,但Windows正在使用JRE,按照" .jar"文件关联。

<强>更新 您可以使用{1}这样的runapp.bat文件运行它:

%JAVA_HOME%\javaw.exe -Xmx1024m -jar JavaEdit.jar

BAT文件必须与您的JAR文件在位于同一文件夹

<强> UPDATE2: 确保已定义JAVA_HOME环境变量。您可以使用以下命令进行检查:

c:\> echo %JAVA_HOME%

它应该打印出JDK的位置 - C:\Program Files(x86)...