ant javac compile throws an exception - ant

I am new to writing ant scripts . I have code in Java 1.4 and when I compile the following ant script
<target depends="init" name="javacompile">
<javac srcdir="${src}" destdir="${dest}" source="1.4" target="1.4"/>
</target>
I am getting an
Unsupported major.minor version 49.0 Exception.
java.lang.UnsupportedClassVersionError: com/sun/tools/javac/Main (Unsupported major.minor version 49.0)

The error is saying that your Ant script requires Java 1.4, but you're running JDK 5.0:
These are the mappings to major/minor numbers:
http://en.wikipedia.org/wiki/Java_class_file
J2SE 8 = 52 (0x34 hex),,
J2SE 7 = 51 (0x33 hex),
J2SE 6.0 = 50 (0x32 hex),
J2SE 5.0 = 49 (0x31 hex),
JDK 1.4 = 48 (0x30 hex),
JDK 1.3 = 47 (0x2F hex),
JDK 1.2 = 46 (0x2E hex),
JDK 1.1 = 45 (0x2D hex).
SUGGESTION:
Remove the source="1.4" target="1.4" qualifiers (if possible).

Related

JENKINS: ERROR when I try to use an older JDK for a specific maven project

I am using the Jenkins version 2.73-1.1 on a CentOS Linux release 7.3.1611 server.
There are 3 different versions of JDK on the server:
[root # jenkins java] # ll
total 12
lrwxrwxrwx. 1 root root 16 27 Apr 16.25 default -> / usr / java / latest
drwxr-xr-x. 8 root root 4096 27 Mar 2013 jdk1.6.0_45
drwxr-xr-x. Root root 4096 11 Apr 2015 jdk1.7.0_80
drwxr-xr-x. 9 root root 4096 27 Apr 16.25 jdk1.8.0_131
lrwxrwxrwx. 1 root root 22 27 apr 16.25 latest -> /usr/java/jdk1.8.0_131
As shown in the pictures below, Jenkins is using the jdk1.8.0_131 version and in my project I specified to use JDK7.
Jenkins setup Image 1
Jenkins setup Image 2
When I try to compile my project (mvn clean deploy), the build fails and I see this ERROR in log:
[Vodafone] $ /usr/java/jdk1.7.0_80/bin/java -cp
/var/lib/jenkins/plugins/maven-plugin/WEB-INF/lib/maven3-agent-1.11.jar:/opt/apache-maven-3.0.4/boot/plexus-classworlds-2.4.jar
org.jvnet.hudson.maven3.agent.Maven3Main /opt/apache-maven-3.0.4
/var/cache/jenkins/war/WEB-INF/lib/remoting-3.10.jar
/var/lib/jenkins/plugins/maven-plugin/WEB-INF/lib/maven3-interceptor-1.11.jar
/var/lib/jenkins/plugins/maven-plugin/WEB-INF/lib/maven3-interceptor-commons-1.11.jar
46349 <===[JENKINS REMOTING CAPACITY]===>channel started ERROR:
================================================================================ ERROR: Invalid project setup: jenkins/security/MasterToSlaveCallable :
Unsupported major.minor version 52.0 ERROR:
[JENKINS-18403][JENKINS-28294] JDK 'JAVA7' not supported to run Maven
projects. ERROR: Maven projects have to be launched with a Java
version greater or equal to the minimum version required by the
master. ERROR: Use the Maven JDK Toolchains (plugin) to build your
maven project with an older JDK. ERROR: Retrying with slave Java and
setting compile/test properties to point to /usr/java/jdk1.7.0_80.
ERROR:
================================================================================ Established TCP socket on 38129 [Vodafone] $
/usr/java/jdk1.8.0_131/jre/bin/java -cp
/var/lib/jenkins/plugins/maven-plugin/WEB-INF/lib/maven3-agent-1.11.jar:/opt/apache-maven-3.0.4/boot/plexus-classworlds-2.4.jar
org.jvnet.hudson.maven3.agent.Maven3Main /opt/apache-maven-3.0.4
/var/cache/jenkins/war/WEB-INF/lib/remoting-3.10.jar
/var/lib/jenkins/plugins/maven-plugin/WEB-INF/lib/maven3-interceptor-1.11.jar
/var/lib/jenkins/plugins/maven-plugin/WEB-INF/lib/maven3-interceptor-commons-1.11.jar
38129 <===[JENKINS REMOTING CAPACITY]===>channel started
Why Jenkins doesn't use jdk 7?
As it is documented on the maven project plugin of jenkins home page:
Jenkins >= 2.54 requires Java 8 thus Maven jobs must be launched with Java >= 8
Luckily the best workaround is just mentioned in your stacktrace:
... Use the Maven JDK Toolchains (plugin) to build your maven project with an older JDK...
In order to compile your project using java 7 and launching successful jenkins job using current jenkins version, I would recommend you to use this plugin by 2 simple steps:
Add toolchains.xml file to your .m2 directory both on developers system and jenkins server
<toolchains>
<toolchain>
<type>jdk</type>
<provides>
<version>1.7</version>
<vendor>openjdk</vendor>
</provides>
<configuration>
<jdkHome>/usr/lib/jvm/java-7-openjdk-amd64</jdkHome>
</configuration>
</toolchain>
<toolchain>
<type>jdk</type>
<provides>
<version>1.8</version>
<vendor>openjdk</vendor>
</provides>
<configuration>
<jdkHome>/usr/lib/jvm/java-8-openjdk-amd64</jdkHome>
</configuration>
</toolchain>
[...]
</toolchains>
Add toolchain plugin to your project pom file
<project>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-toolchains-plugin</artifactId>
<version>1.1</version>
<executions>
<execution>
<goals>
<goal>toolchain</goal>
</goals>
</execution>
</executions>
<configuration>
<toolchains>
<jdk>
<version>1.7</version>
<vendor>openjdk</vendor>
</jdk>
</toolchains>
</configuration>
</plugin>
</plugins>
</build>
</project>
Finally config your jenkins job to use jdk 8, toolchain will take care of compiling your project with jdk 7.
Try configuring a Freestyle Job in Jenkins. You will need to select the "Invoke top-level Maven targets" build. I was having a similar problem. This worked for me.

Grails grails.project.target.level config has no effect?

How do I compile my groovy source code into java 1.8 bytecode?
I'm using Grails 2.5.4, Java 1.8.0_74
I might be missing something obvious here, but the below settings in Grails BuildConfig.groovy don't seem to affect the Java bytecode being generated for Groovy source files.
Major version 49=Java 1.5, and 52=Java 1.8 (cite: how to check the jdk version used to compile a .class file)
grails.project.target.level = 1.8
grails.project.source.level = 1.8
Then to check bytecode version:
$ javap -verbose target/classes/MyGroovy.class | grep major
(standard input):7: major version: 49
$ javap -verbose target/classes/MyJava.class | grep major
(standard input):7: major version: 52
$ javap -verbose target/classes/UrlMappings.class | grep major
(standard input):7: major version: 49
$ javap -verbose target/classes/BootStrap.class | grep major
(standard input):7: major version: 49
So based on the above results, it seems that the target.level=bla configuration does affect Java source code, but not Groovy?
My test setup:
Vanilla grails 2.5.4 app: 'grails create-app foo'
Added one java source file, and one groovy. Placed in foo/src/*
Modified BuildConfig.groovy and set target/source levels to '1.8' instead of the default '1.6'
Those are all the steps. Totally simple.
My source code:
class MyJava {
String name;
int age;
public static String foo() { return "java!"; }
}
And...
class MyGroovy {
def name
def age
static foo() { 'groovy!' }
}

Mozilla rhino not compiling

I am trying to compile commit 9c8bad6ec6d51a0abfb9f6e840e2dbec3e2fbcee of the Mozilla Rhino project to make some tests on it.
I cloned the repository to my server, and checked out the hash.
I then proceeded into compiling using ant, which returned errors on lines 128, 57 and 53 of build.xml.
I'm not 100% sure of what line 127 does:
<!--<antcall target="jar-src"/>-->
So I commented it out for the time being.
For lines 53 and 57:
<target name="compile-src" depends="prepare">
<ant dir="/home/dario/testEnv/181834/prefix/rhino"/> <- line 53
</target>
<target name="compile-toolsrc" depends="prepare">
<ant dir="/home/dario/testEnv/181834/prefix/rhino"/> <- line 57
</target>
They seemed to be setting the path for compiling the path. They did so dynamically originally, with some variable, but as that didn't work I changed it and wrote the actual path.
Not when I compile it doesn't throw any errors, but it does not generate the compiled classes.
Any ideas?
That version of Rhino is ancient.
You could fix your problem by using JDK version 1.4 to compile. The problem is that that version of Rhino contained code that was incompatible with the JDK 1.5 enhancements (specifically, it attempts to use enum as a variable name).
Alternatively, you could patch the build.xml to specify the source version to be 1.4 when compiling on the appropriate javac tasks.
More realistically, do you have any option of upgrading to a reasonably recent version of Rhino?

Ant 1.8.1 showing incorrect java version?

I'm running Ant 1.8.1 under Win 7. And my Java version is 1.7_17.
I add the following 2 lines in my build file for diagnosing.
<echo message="java.version = ${ant.java.version}" />
<echo message="java.home = ${java.home}" />
And the result is
[echo] java.version = 1.6
[echo] java.home = C:\Program Files\Java\jre7
Any idea on what's going on?
Is it hardcoded in Ant 1.8.1 that the highest java version it can show is 1.6?
I found this from Ant website. It's hardcoded actually.
ant.java.version the JVM version Ant detected; currently it can hold
the values "1.2", "1.3",
"1.4", "1.5" and "1.6".
After I update my Ant to 1.9.0, the issue is solved.
[echo] java.version = 1.7
[echo] java.home = C:\Program Files\Java\jdk1.7.0_17\jre
It's kind of funny. :)
It looks like you have JAVA_HOME set correctly, but do you have %JAVA_HOME%\bin included in your PATH environment variable ? It looks like you're currently picking up the java.exe from a Java 6 installation.
I think ant.java.version shows the Java version your Ant was built with. The actual version of your java installation is stored in java.version.

Including SVN head revision number in the manifest.mf

I am creating a WAR file using Ant. When WAR is created it has MANIFEST.MF file with the default content :
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.8.4
Created-By: 1.7.0_07-b10 (Oracle Corporation).
So, when the WAR is created it should also have Build Revision Number/Head revision number of SVN and the MANIFEST.MF in the WAR should look like this:
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.8.4
Created-By: 1.7.0_07-b10 (Oracle Corporation)
Revision: #Head Revision number
I would recommend to use buildnumber-maven-plugin. See this article for more details: http://manifests.jcabi.com/versioning.html
Also, see this post for more details: http://www.yegor256.com/2014/07/03/how-to-read-manifest-mf.html

Resources