[编辑:]尝试使用批处理脚本运行.jar后我发现
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager fileManager fileManager = compiler.getStandardFileManager(null, Locale.getDefault(), null);
仍会导致NullpointerException。所以设置" java.home"当程序从exe运行时,无论是从包批处理还是从批处理脚本开始,它都不会以这种方式工作。 有人知道解决方法吗? [/编辑]
我有一个程序,用户可以导入将在运行时编译的java代码。要使用JavaCompiler,需要将java.home的属性设置为已安装的JDK:
System.setProperty("java.home", [USER-ENTERED-PATH-TO-JDK);
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
fileManager = compiler.getStandardFileManager(diagnostics, null, null);
...
当程序作为JAR启动时,这可以正常工作。但是我将它包装起来以使用launch4j部署到.exe。如果用户将路径设置为" java.home" (。例如C:\ Program Files \ Java \ jdk1.8.0_91).exe中出现以下异常:
Exception in thread "AWT-EventQueue-0" java.lang.Error: java.io.FileNotFoundException: C:\Program Files\Java\jdk1.8.0_91\lib\tzdb.dat
at sun.util.calendar.ZoneInfoFile$1.run(ZoneInfoFile.java:261)
at java.security.AccessController.doPrivileged(Native Method)
at sun.util.calendar.ZoneInfoFile.<clinit>(ZoneInfoFile.java:251)
at sun.util.calendar.ZoneInfo.getTimeZone(ZoneInfo.java:589)
at java.util.TimeZone.getTimeZone(TimeZone.java:560)
at java.util.TimeZone.setDefaultZone(TimeZone.java:666)
at java.util.TimeZone.getDefaultRef(TimeZone.java:636)
at java.util.Date.normalize(Date.java:1197)
at java.util.Date.toString(Date.java:1030)
at java.util.Properties.store0(Properties.java:829)
at java.util.Properties.store(Properties.java:818)
at GenericTool.Config.writeConfig(Config.java:192)
at GUI.MainWindow$20.actionPerformed(MainWindow.java:1211)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.AbstractButton.doClick(AbstractButton.java:376)
at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:833)
at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:877)
at java.awt.Component.processMouseEvent(Component.java:6535)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6300)
at java.awt.Container.processEvent(Container.java:2236)
at java.awt.Component.dispatchEventImpl(Component.java:4891)
at java.awt.Container.dispatchEventImpl(Container.java:2294)
at java.awt.Component.dispatchEvent(Component.java:4713)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
at java.awt.Container.dispatchEventImpl(Container.java:2280)
at java.awt.Window.dispatchEventImpl(Window.java:2750)
at java.awt.Component.dispatchEvent(Component.java:4713)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
Caused by: java.io.FileNotFoundException: C:\Program Files\Java\jdk1.8.0_91\lib\tzdb.dat
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:195)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at sun.util.calendar.ZoneInfoFile$1.run(ZoneInfoFile.java:255)
... 50 more
can't open C:\Program Files\Java\jdk1.8.0_91\lib\tzmappings.
在设置java.home的路径时似乎无法识别JDK,因此程序的功能不再有效,必须重新启动。在以jar作为程序执行程序时执行相同操作不会导致任何问题。 有人知道解决方案吗?