Ant build for Intellij-IDEA GUI forms

2020-03-24 08:29发布

问题:

I have a project using forms, it compiles and runs just fine, but when compiled with the generated ant it doesn't run

my ant file has the java2 task and i added the needed jars to my lib folder

  <property name="javac2.home" value="lib/idea"/>
  <path id="javac2.classpath">
    <pathelement location="${javac2.home}/javac2.jar"/>
    <pathelement location="${javac2.home}/jdom.jar"/>
    <pathelement location="${javac2.home}/asm.jar"/>
    <pathelement location="${javac2.home}/asm-commons.jar"/>
    <pathelement location="${javac2.home}/jgoodies-forms.jar"/>
  </path>
  <taskdef name="javac2" classname="com.intellij.ant.Javac2" classpathref="javac2.classpath"/>
  <taskdef name="instrumentIdeaExtensions" classname="com.intellij.ant.InstrumentIdeaExtensions" classpathref="javac2.classpath"/>

but when I run my program I get the following error:

Exception in thread "main" java.lang.NoClassDefFoundError: com/intellij/uiDesigner/core/Spacer
Caused by: java.lang.ClassNotFoundException: com.intellij.uiDesigner.core.Spacer
    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:307)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:248)

I have noticed that the version compiled from the Idea has some classes in the output folder (what is missing) while the ant generated version doesn't have those classes.

EDIT: i implemented the solution from below, this means I modified my ant build to extract the content of forms_rt.jar to my output folder

<unjar src="${javac2.home}/forms_rt.jar" dest="${webserver.output.dir}">
          <patternset>
              <include name="**/*.class"/>
          </patternset>
</unjar>

回答1:

You need to include forms_rt.jar from IDEA_HOME\redist into the classpath of your application, either via manifest or by unpacking the classes from this jar and packaging them together with the final jar of your application.

When you compile from IDEA, it copies the required classes to the output automatically (it's controlled via an option in Settings | GUI Designer).