1

我已经创建了在 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);
4

2 回答 2

3

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

来自JavaDoc

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

于 2013-04-14T16:53:23.460 回答
3

a 唯一可能的地方NullPointerException是这一行:

StandardJavaFileManager fileManager = compiler.getStandardFileManager(c,

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

我的猜测是您JAVA_HOME指向 JDK 并且在PATH.jar 文件中,但 Windows 正在使用 JRE,根据“.jar”文件关联。

更新: 您可以使用这样的runapp.bat文件运行它:

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

BAT 文件必须与JAR 文件位于同一文件夹中。

UPDATE2: 确保您已定义JAVA_HOME环境变量。你用这个命令检查:

c:\> echo %JAVA_HOME%

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

于 2013-04-14T16:56:23.663 回答