これに似た質問が約1年前に返されました。しかし、私は、特定の質問にいくつかの欠落した詳細とポイントがあり、適切な答えが与えられていないことがわかります。だから、私は同じことを再起しています。Mavenを使用したIntelliJ IDEAのGUIアプリケーション:jarファイルに組み込まれた後にNullPointerExceptionがスローされる
私はIntelliJ IDEAでMavenプロジェクトを作成し、その中にGUIフォームを追加しました。デフォルトでは、IDEAはフォームの構造用に別個のXMLファイルを作成します。鉱山は次のように現れます。
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="TestUI">
<grid id="27dc6" binding="panel1" default-binding="true" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="20" y="20" width="500" height="400"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="e40aa" class="javax.swing.JTextField" binding="txtTest">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="150" height="-1"/>
</grid>
</constraints>
<properties/>
</component>
</children>
</grid>
</form>
これには、次のように表示されるクラスがあります。
import javax.swing.*;
public class TestUI {
private JTextField txtTest;
private JPanel panel1;
public TestUI() {
String str = txtTest.getText();
JOptionPane.showMessageDialog(null, str);
createAndShowGui();
txtTest.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "Clicked");
}
});
}
private void createAndShowGui(){
JFrame frame = new JFrame("test");
frame.setContentPane(panel1);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
TestUI obj = new TestUI();
}
}
このプロジェクトのPOM.xmlは元々次のようになります。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.company.test</groupId>
<artifactId>UITestWithIJ</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.6</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>TestUI</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
これをIDEAで実行すると、問題なく動作します。だからアセンブリプラグインを使用してMaven経由でファットjarファイルにビルドしました。その後、問題は飛び出し始めました。 Mavenによって作成されたjarが実行されると、次のように表示されるNullPointerExceptionがスローされます。
[email protected]:~/.m2/repository/com/company/test/UITestWithIJ/1.0-SNAPSHOT$ java -jar UITestWithIJ-1.0-SNAPSHOT-jar-with-dependencies.jar
Exception in thread "main" java.lang.NullPointerException
at TestUI.<init>(TestUI.java:13)
at TestUI.main(TestUI.java:34)
私は掘っ深く、私はこの理由はそれにtxtTest
識別子を有するされていることライン#13であることを見出しました。この識別子は何も意味がありません。したがって、デフォルト値はnull
です。だから問題は実際にIDEAであることに気づき、IDEAによって生成された.form
ファイルを、JVMだけが読むことができる形式に変換するだけです。
研究の結果、候補ソリューションhereが見つかりました。そこの指示に従って、私はPOM.xmlを次のように変更しました。
は、次の依存関係を追加しました
<dependency> <groupId>com.intellij</groupId> <artifactId>forms_rt</artifactId> <version>7.0.3</version> </dependency>
これは、問題を解決した、以下のプラグイン
<plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>ideauidesigner-maven-plugin</artifactId> <executions> <execution> <goals> <goal>javac2</goal> </goals> </execution> </executions> <configuration> <fork>true</fork> <debug>true</debug> <failOnError>true</failOnError> </configuration> </plugin>
を追加しました。しかし、TestUI.javaには、イベントリスナーが追加されています。これは、Java 8からのラムダ式に置き換えることができます。私がその置き換えをやり終えたら、Mavenはjarを構築するのに失敗し、以下のエラーがスローされます。
[email protected]:~/IdeaProjects/UITestWithIJ$ mvn clean install
[INFO] Scanning for projects...
[WARNING]
[WARNING] Some problems were encountered while building the effective model for com.company.test:UITestWithIJ:jar:1.0-SNAPSHOT
[WARNING] 'build.plugins.plugin.version' for org.codehaus.mojo:ideauidesigner-maven-plugin is missing. @ line 53, column 21
[WARNING]
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING]
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING]
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building UITestWithIJ 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ UITestWithIJ ---
[INFO] Deleting /home/user/IdeaProjects/UITestWithIJ/target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ UITestWithIJ ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.6.0:compile (default-compile) @ UITestWithIJ ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 1 source file to /home/user/IdeaProjects/UITestWithIJ/target/classes
[INFO]
[INFO] --- ideauidesigner-maven-plugin:1.0-beta-1:javac2 (default) @ UITestWithIJ ---
[INFO] Executing IDEA UI Designer task...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.227 s
[INFO] Finished at: 2016-12-29T11:11:43+05:30
[INFO] Final Memory: 17M/205M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.codehaus.mojo:ideauidesigner-maven-plugin:1.0-beta-1:javac2 (default) on project UITestWithIJ: Execution default of goal org.codehaus.mojo:ideauidesigner-maven-plugin:1.0-beta-1:javac2 failed: 52264 -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/PluginExecutionException
私は-Xスイッチと同じに引退し、どのような実際にここで失敗すると、プラグインideauidesigner-maven-plugin
用のMavenのゴールjavac2
であり、そのための唯一の原因は、ラムダ式であることを見出しました。プラグインのメソッドでArrayIndexOutOfBoundsException
がスローされました。これと依存関係forms_rt
がMavenリポジトリにあり、2009年と2010年に最後に更新されたことに気付きました。プラグインでラムダ式を処理できないことがあります。
以下は私の質問です。
- 何か間違っていますか?
- これに対応する修正プログラムはありますか?
- これに対応する選択肢はありますか?
解決策は見つかりましたか? – Nikola