4

I'm using an ant build.xml that generates classes and compiles them with the tools.jar from the JDK.

I am using MAVEN 2.2.1 version. JDK 1.5 to execute MAVEN. As maven 2.2.1 version supports higher then JDK 1.5 only so I have to use it.

This maven-antrun-plugin doesn't allow to specify neither source or target version for the compiler. So, the generated classes are compiled against the currently running JVM which is JDK , using its rt.jar and the tools.jar provided in plugin dependencies (or placed in the lib/ext directory of the jvm).

As Maven is executed in a 1.5 JVM (jdk1.5.0_22) is needed for my project to compile these classes with JDK 1.4.2 version because the server where I want to deploy them is running on a 1.4 JVM, so I am getting exception while I am compiling with MAVEN plugin from my eclipse or command line.

I couldn't find a way to tell antrun to compile my classes using a different java version. I tried the following workarounds :

  • change the tools.jar dependency to point to a 1.4 version => as the compiler uses the rt.jar from the currently running 1.5 JVM, the class file version doesn't match (version 49.0, expecting 48.0)
  • add a dependency to a 1.4 rt.jar => it doesn't change anything, as the rt.jar should be specified in the boot classpath.

Following one is the sample code which i am using.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.6</version>
    <executions>
        <execution>
            <id>install</id>
            <phase>install</phase>
            <goals>
                <goal>run</goal>
            </goals>
            <configuration>
                <source>${java-version}</source>
                <target>${java-version}</target>
                <compilerVersion>${java-version}</compilerVersion>
                <compilerArguments>
                    <classpath>${java.home}/lib/tools.jar</classpath>
                    <classpath>${java.home}/jre/lib/rt.jar</classpath>
                </compilerArguments>
                <tasks>
                    <ant antfile="WPSEjb_build.xml"/>
                </tasks>
            </configuration>
        </execution>
    </executions>
    <dependencies>
        <dependency>
            <groupId>crimson</groupId>
            <artifactId>crimson</artifactId>
            <version>1.1.3</version>
        </dependency>
        <dependency>
            <groupId>sun.jdk</groupId>
            <artifactId>tools</artifactId>
            <version>1.4.2</version>
            <scope>system</scope>
            <systemPath>${java.home}/lib/tools.jar</systemPath>
        </dependency>
        <dependency>
            <groupId>com.sun</groupId>
            <artifactId>rt</artifactId>
            <version>${java-version}</version>
            <scope>system</scope>
            <systemPath>${java.home}/jre/lib/rt.jar</systemPath>
        </dependency>
    </dependencies>
</plugin>

I am getting exceptions something like this.

WPSClient.java:22: cannot access java.lang.Object
 [wlwBuild] [Build] bad class file: C:\Java\jdk1.5.0_22\jre\lib\rt.jar(java/lang/Object.class)
 [wlwBuild] [Build] class file has wrong version 49.0, should be 48.0
 [wlwBuild] [Build] Please remove or make sure it appears in the correct subdirectory of the classpath.
 [wlwBuild] [Build] public static WPSServerRemote getWPSServer() throws MitchellException {
 [wlwBuild] [Build] ^
 [wlwBuild] [Build] 3 errors
 [wlwBuild] [Build] BUILD FAILED
 [wlwBuild] [Build] Compile failed; see the compiler error output for details.
 [wlwBuild] [Build] 
 [wlwBuild] java.lang.reflect.InvocationTargetException
 [wlwBuild] java.lang.reflect.InvocationTargetException
 [wlwBuild]     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 [wlwBuild]     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
 [wlwBuild]     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
 [wlwBuild]     at java.lang.reflect.Method.invoke(Method.java:592)
 [wlwBuild]     at workshop.core.Compile.start(Compile.java:19)
 [wlwBuild]     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 [wlwBuild]     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
 [wlwBuild]     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
 [wlwBuild]     at java.lang.reflect.Method.invoke(Method.java:592)
 [wlwBuild]     at workshop.core.Starter.invokeStart(Starter.java:34)
 [wlwBuild]     at workshop.core.Compile.main(Compile.java:9)
 [wlwBuild] Caused by: java.lang.NoClassDefFoundError: org/apache/crimson/tree/XmlDocument
 [wlwBuild]     at workshop.util.ide.PreferencesNode._export(PreferencesNode.java:540)
 [wlwBuild]     at workshop.util.ide.PreferencesNode.exportSubtree(PreferencesNode.java:820)
 [wlwBuild]     at workshop.util.ide.PreferencesNode.flush(PreferencesNode.java:984)
 [wlwBuild]     at workshop.core.App$15.run(App.java:1000)
 [wlwBuild]     at workshop.core.asynctask.AsyncTaskManager.showDialogWhileRunning(AsyncTaskManager.java:272)
 [wlwBuild]     at workshop.core.asynctask.AsyncTaskManager.showDialogWhileRunning(AsyncTaskManager.java:482)
 [wlwBuild]     at workshop.core.App.exit(App.java:994)
 [wlwBuild]     at workshop.core.CompileHelper.compile(CompileHelper.java:298)

Please let me know how can I setup JDK 1.4.2 Version to Compile my classes by using maven-antrun-plugin.

Jon7
  • 7,165
  • 2
  • 33
  • 39
user886614
  • 375
  • 1
  • 4
  • 14

2 Answers2

0

Newer versions of the JDK can compile code to be run on older JDKs. Just set your target to 1.4 if that's the version of the JDK on your server.

You might want to check out the java compiler usage guide for 1.5 and check out the target option to get a better understanding of how the compiler handles this.

EDIT: To clarify, the part of your pom that calls out the target version is ${java-version}. That will be passed to javac as the target option. You'll want to tell it 1.4, since minor version numbers are not used.

Jon7
  • 7,165
  • 2
  • 33
  • 39
  • Hi Jon7, I am trying to use following Weblogic Workshop command from my Build.xml and I am can’t provide javac information under command so I am looking to setup Compiler information under maven-antrun-plugin, But till now I didn’t get any answer how can I provide different compiler information for this plugin. My build.xml information as below. How to provide jdk 1.4.2 as compiler under maven-antrun-plugin – user886614 Aug 11 '11 at 16:13
  • SO you mean that instead of 1.4.2 i have to pass 1.4 ?? – user886614 Aug 13 '11 at 07:05
  • Yes. There is no reason to use an old jdk. All new jdks can build for old versions of java. – Jon7 Aug 13 '11 at 07:56
  • It's not working....i can't compile JDK 1.4 or 1.4.2 for ant plugin. I mean i am not able too apply it. There is something wrong with Ant-Plugin. or may be i am not getting correct answer. – user886614 Aug 18 '11 at 23:16
  • 1
    Please explain what you mean by "it's not working" and "i am not able to apply it." If you're getting an error message, please post it. – Jon7 Aug 18 '11 at 23:21
-2

I am able to build it by using following command.

          <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.2</version>
            <executions>
              <execution>
                <id>install</id>
                <phase>install</phase>
                <goals>
                  <goal>exec</goal>
                </goals>
              </execution>
            </executions>
            <configuration>             
              <executable>C:\test\Ejb.bat</executable>
            </configuration>
          </plugin>

I am calling ant -f build.xml build command under Ejb.bat file. Which is using all configuration which i am giving under build.xml so i am able to build with JDK 1.4.2 version now.

agf
  • 171,228
  • 44
  • 289
  • 238
user886614
  • 375
  • 1
  • 4
  • 14