ant xjc task to work with jdk 6 - ant

I am using xjc ant task to generate java classes from xsd and wsdl and then i am generating a jar out of these generated classes. PFB the code i am using,
<!-- Below code generates java classes from wsdl -->
<exec executable="xjc">
<arg value="-wsdl" />
<arg value="${wsdl}/mysample.wsdl" />
<arg value="-d" />
<arg value="${myclasses}" />
<arg value="-d" />
<arg value="${myclasses}" />
<arg value="${xsdfile}/mysample.xsd" />
</exec>
<!-- Below code generates jar from java classes -->
<jar destfile="${jars}/mysample.jar" basedir="${myclasses}"/>
It works fine in my local with JDK7 but when i deploy it to my server which has jdk6 i am getting <pre>org.jboss.resteasy.spi.UnhandledException: java.lang.UnsupportedClassVersionError: com/my/webservices/mysampleweb. Any suggestion how to make this work??
EDIT:
I am getting major version 51 is newer than 50, the highest major version supported by this compiler. error for some classes which were generated from xsd. How to resolve this??
I checked my ANT_HOME,java version and jre everything is fine.. But still i am getting the above error when build using ant via command line.. Any suggestion

If you're building with Java version 7 and trying to run it on Java 6 - or really anything earlier than the version of Java you compiled with - you'll get this UnsupportedClassVersionError. Compiled Java bytecode is forwards-compatible with new releases of the JVM, but not backwards compatible. Put Java 7 on your runtime environment, or compile using JDK 6.
Specifically, from Oracle's documentation of the Java 7 release:
Binary Compatibility
Java SE 7 is binary-compatible with Java SE 6 except for the incompatibilities listed below. Except for the noted incompatibilities, class files built with the Java SE 6 compiler will run correctly in Java SE 7.
The class file version for Java SE 7 is 51, as per the JVM Specification, because of the invokedynamic byte code introduced by JSR 292. Version 51 class files produced by the Java SE 7 compiler cannot be used in Java SE 6.

Related

Run ant task in different jvm

Our ant build is run using Java 1.7.0 for JAVA_HOME. This way javac and all other Java dependent targets use the correct Java by default.
But 1 ant target from an external supplier does not support (or rather has a bug) using Java 1.7.0. And unlike e.g. javac or a forked junit, this target does not support parameters to switch jvm.
Is it possible to run a specific ant target in a different jvm?
To make Jeanne Boyarsky's suggestion of using the exec Ant task concrete, the following example wraps the exec task in a macro to simplify calling targets with various JVMs. Notice that the JVM is set using the Ant environment variable JAVACMD.
Example Project
<?xml version="1.0" encoding="UTF-8"?>
<project name="run-target-with-specified-java-version" default="test">
<macrodef name="exec-target">
<attribute name="antfile" default="${ant.file}" />
<attribute name="target" />
<attribute name="jvm" default="${java.home}/bin/java" />
<sequential>
<exec executable="ant">
<env key="JAVACMD" value="#{jvm}" />
<arg line='-f "#{antfile}"' />
<arg line="#{target}" />
</exec>
</sequential>
</macrodef>
<target name="echo-java-version">
<echo message="Java version: ${java.version}" />
</target>
<target name="test">
<exec-target target="echo-java-version" />
<property name="java1.6"
location="/usr/lib/jvm/jdk1.6/bin/java" />
<exec-target target="echo-java-version" jvm="${java1.6}" />
</target>
</project>
Output
test:
[exec] Buildfile: /home/your/project/build.xml
[exec]
[exec] echo-java-version:
[exec] [echo] Java version: 1.7.0
[exec]
[exec] BUILD SUCCESSFUL
[exec] Total time: 0 seconds
[exec] Buildfile: /home/your/project/build.xml
[exec]
[exec] echo-java-version:
[exec] [echo] Java version: 1.6.0
[exec]
[exec] BUILD SUCCESSFUL
[exec] Total time: 0 seconds
BUILD SUCCESSFUL
Total time: 2 seconds
You can use the exec task to run the build file with that target defined to run as a parameter. It could be running in a different JVM since you can pass the JVM to that exec call.
Note that you'd have to refactor the target to rely on files for communication rather than setting properties. Since it would be in a different JVM, it obviously can't rely on memory.
You can run a target in a different JVM (we do it all the time). You just need to use fork:
<javac srcdir="${src}"
destdir="${build}"
fork="yes"
/>
But I sense you are aware of this, so how about running the external ANT task as it is, and rest of them (lets say you have 3 more javac tasks) in the JVM you want. This can be achieved by setting a property file. See javac task
It is possible to use different compilers. This can be specified by either setting the global build.compiler property, which will affect all tasks throughout the build
So this property will affect your 3 tasks and run them in the JVM you specified (say 1.7) and you can set the default JAVA_HOME to whatever your external library task needs.

Using Gradle to generate jax-ws java sources

I have a wsdl and I'd like to generate jax-ws type Java source from it using IBM Websphere version of wsimport. How can I do this in an easy way? wsimport.bat references com.ibm.ws.jaxws.tools.WsImport to do the code generation.
I solved the problem by calling wsimport directly. Just make sure websphereHome is set to the websphere home folder on your machine. Then genDir is the folder where you want the files to be generated to. Finally, wsdlFile is the path to the wsdl used for generation.
task generateWSDL2Java(type:Exec) {
doFirst{
genDir.mkdirs()
}
cmd = websphereHome + '/bin/wsimport.bat -keep -d '+genDir+' '+wsdlFile
commandLine = ['cmd', '/K', cmd]
}
Here's a simple Ant script, using a WebSphere 6.1 runtime (with the WebSphere Feature Pack, which is required for JAX-WS), which I just tested:
<?xml version="1.0" encoding="UTF-8"?>
<project name="JAX-WS Client">
<property name="was.dir" value="C:\Program Files (x86)\IBM\WebSphere\AppServer"/>
<path id="jaxws.gen.classpath">
<fileset dir="${was.dir}/plugins">
<include name="*com.ibm.wsfp.main_6.1.0.jar" />
<include name="*org.apache.axis2_6.1.0.jar" />
<include name="*com.ibm.jaxws.tools_6.1.0.jar" />
<include name="*com.ibm.jaxb.tools_6.1.0.jar" />
</fileset>
<fileset file="${was.dir}/lib/j2ee.jar"/>
</path>
<!-- Ant task definition for wsimport -->
<taskdef classpathref="jaxws.gen.classpath" name="wsimport" classname="com.sun.tools.ws.ant.WsImport"/>
<target name="wsimport">
<wsimport sourcedestdir="./src" destdir="./build" debug="true" verbose="true"
keep="true" wsdl="${wsdlFile}" />
</target>
</project>
If you have RAD 8, here's the InfoCenter article which describes using the JAX-WS Ant tasks from within that. I'm not sure how other WebSphere development environments compare.
JAX-WS artifacts are portable, which means that you are not required to use IBM's tools. BTW, I think that the wsgen and wsimport tools shipped with WAS actually use code from the Sun/Oracle reference implementation.
Therefore you could use any documented solution for Gradle, even if it is not WebSphere specific.

ant builds running through maven issue

So I'm building a project with maven, and in this maven pom we have a reference to an ant build script. The maven pom triggers this ant build to build a project (an install of alfresco with mysql database and tomcat server packed up with it).
The issue seems to be when you try to set up a database for alfresco to use through the ant build. This is the part of the ant build.
<target name="createDatabase">
<exec spawn="false" executable="${mysql.home}/bin/mysql" failonerror="true">
<arg value="-u" />
<arg value="root" />
<arg value="-e" />
<arg value="source ${alfresco.home}\mysql\db_setup.sql" />
</exec>
</target>
I'm getting 'unknown command '\U' sent back to me as an error on this. Of course you can install the DB manually but I want it as part of this ant script. SOmeone I work with runs this successfully on XP, but I'm getting that error on win7. Any ideas?

how to build java me CDC app with ant? can i build using javaSE

Hi im trying to build our java me app that works on CDC base, I have seen antenna, but cant get it to work, it seems to be geared towards CLDC, but i cant make it work, and am wondering if i need to do anything specific to make it compile a CDC app?
or if there is another way to build CDC apps with ant?
You don't really need antenna feature, cdc app is like to JavaSe app.You could use plain ant for building cdc apps. JavaSE javac + cdc libraries is enought.
F.e.
<target name="app" >
<mkdir dir="${dir.build.compiled}" />
<javac classpathref="app.cp"
fork="true" debug="on"
srcdir="<sources>"
source="1.3"
target="1.3"
destdir="${dir.compiled}"/>
<jar destfile="${app.jar}" basedir="${dir.compiled}" />
</target>

Setting the target version of Java in ant javac

I need to compile a jar file using ant (1.7.0) to run under a specific version of Java (1.5). I currently have Java 1.6 on my machine. I have tried setting:
<target name="compile">
<javac compiler="javac1.5" target="1.5" srcdir=.../>
</target>
I have also removed
<property name="build.compiler" value="modern"/>
and there is no properties file. I am running Java 1.6 on Linux/SUSE
Also is there a simple way of determining which version of Java is expected in the jar file.
Use "target" attribute and remove the 'compiler' attribute. See here. So it should go something like this:
<target name="compile">
<javac target="1.5" srcdir=.../>
</target>
Both source and target should be specified. I recommend providing ant defaults, that way you do not need to specify source/target attribute for every javac task:
<property name="ant.build.javac.source" value="1.5"/>
<property name="ant.build.javac.target" value="1.5"/>
See Java cross-compiling notes for more information.
To find the version of the java in the classfiles I used:
javap -verbose <classname>
which announces the version at the start as
minor version: 0
major version: 49
which corresponds to Java 1.5
You may also set {{ant.build.javac.target=1.5}} ant property to update default target version of task.
See http://ant.apache.org/manual/javacprops.html#target

Resources