JDK compiler version issue - java

I'm using Ant to compile Java.The project has to be compiled using JDK 1.5 , however some part of the code references a package compiled with JDK 1.6 version.
I set the JAVA_HOME to 1.5 , error is thrown at this reference as
[javac] class file has wrong version 50.0, should be 49.0
What is the way out without downgrading the reference version to 1.5

What you are asking isn't possible. You should compile your library with JDK 1.5.

Once Java has been compiled at a certain version, you cannot use that version on older versions of Java.
The package you are using might have a version available which is suitable for an older version of Java, alternatively you may be able to get the source code and recompile with the older version (if it doesn't use any Java 6 libraries / features).

Think this way:
The library you use has been compiled using JDK 6. It may be using some features introduced in Java 6 (that were not part of previous versions of Java)
When you use JDK 5 to compile and run, what do you expect the compiler (and the runtime) to do when this "new" feature is encountered? The JDK 5 does not know this feature and will be "confused"
To avoid this confusion at runtime, the compile itself fails.
You have two options:
Compile your project using JDK 6.
Get a JDK 5 compiled version of your library
If your project were using JDK 6 and the library was compiled with JDK 5, then you wouldn't have faced this issue because of backwards compatibility.

Related

Downloaded JDK 17 but project is using Java 11?

I am new to Java and am a bit confused about how this is working/how I should be working. I am using intellij and a project that I am working on its pom.xml has:
<java.version>11</java.version>
<maven.compiler.source>$(java.version)</maven.compiler.source>
<maven.compiler.target>$(java.version)</maven.compiler.target>
When I go into the project structure on intellij the module is using language level 11.
on my computer I just downloaded the newest JDK (17)?
Does this cause issues working like this? Should I only be using a JDK that is associated with the version I am working?
I have not had any issues... but I am afraid my dependencies might be different than the ones I should be using...or the the build will be different if someone else is using another jdk.
The JDK version specified in the pom.xml specifies what source and target version is passed to javac. This specifies what system libraries and language features can be used.
The language level from IntelliJ matches this.
The installed JDK is the program (or set of programs) used for compiling and running the application.
Java allows to use a newer JDK to compile programs for older (source/target/release) versions.
The produced class files (bytecode) should be the same no matter what JDK version you use as long as the target/release version (specified in the pom.xml/language level in IntelliJ) is the same.
Furthermore, Java is (almost completely) backwards compatible. When writing code for an old Java version, it will likely also work in newer Java versions.

How to validate dependencies' java version when compiling using higher version JDK?

We're using java 8 for most modules/projects, but for some of the modules, we use java 6 (customer requirements).
The developers have java 8 installed and we compile the java 6 projects using these flags:
compileJava {
sourceCompatibility = 1.6
targetCompatibility = 1.6
}
We thought we're all good until we upgraded guava from v20 to latest - 28.1-jre.
To our surprise, the build was successful but failed at runtime.
We have a workaround for building for java 6 using a specific javac found in JDK 6. See more info here. This workaround wields the error class file has wrong version 52.0, should be 50.0 in compile time. The downside is that it requires a download+config+usage of JDK 6 for developers.
Is there a way to validate the dependencies' java version at compile time when using a higher java version? (without installing lower version java) Thanks.
Setting -source and -target values to 1.6 is insufficient to ensure that the resulting output is compatible with 1.6. The program itself must not have any library API dependencies on later versions, and the -source and -target options don't do that. (GhostCat said pretty much the same thing.)
For example, in Java 8, ConcurrentHashMap added a covariant override for the keySet method that returns a new type ConcurrentHashMap.KeySetView. This type didn't exist in earlier versions of Java. However, in the class binary, the return type is encoded at the call site. Thus, even if the source code is compiled with -source 1.6 -target 1.6, the resulting class file contains a dependency on the Java 8 class library API.
The only solution to this is to ensure that only Java 1.6 compatible libraries are in the classpath at compile time. This can be done using the -Xbootclasspath option to point to a JDK 1.6 class library, or it might be simpler just to use a JDK 1.6 installation in the first place.
This applies to external libraries in addition to the JDK, as you've discovered with Guava. The Animal Sniffer project provides plugins for Ant and Maven that checks library dependencies for version problems. Offhand I don't know if there is something similar for Gradle. There might be a way to get Animal Sniffer to work with Gradle, but I have no experience with doing that.
Is there a way to validate the dependencies' java version at compile time when using a higher java version? (without installing lower version java).
You specify your dependencies. When you tell your built system to explicitly use some library X in version Y, then you made a very clear statement.
And you see, it is not only about the class file version number. What if some person doesn't pay attention, and compiles something with Java8 ... with Java6 target, but forgets that the code bases uses Java8-only API calls?!
In other words: you are looking in the wrong place.
The person who makes updates to the build description, and changes a library version from Y to Y+8, that person needs to carefully assess that change. For example by reading release letters.
I agree that a really clever build system could check if libraries you are using come in with a matching class file version. But as said, that is only one aspect of the problem. So instead of looking into a technical solution, I think the real answer is: don't step version numbers because you can, but because you have to. And that manual step of changing that version number, that is something that requires due diligence (on the side of the human doing it).
Thus: I think the most sane approach here is to compile the Java6 deliverables within their own specific build setup. Which you only touch after careful inspection of such details. And sure: convince your customer to move on, and give up a long dead version of Java.

Drools rule compilation error on upgrade to Java 7

I'm using Drools (5.5.0) rules in my Java project (managed using maven 2.2.1). It works fine in Java 6 (1.6.0_45), but when I move to Java 7 (1.7.0_51) and build, certain rules give a rule compilation error such as the following:
Rule Compilation error : [Rule name='SampleRuleName']
com/sample/event/rules/simple/Rule_SampleRuleName46467274.java (2:486) : Syntax error, static imports are only available if source level is 5.0
com/sample/event/rules/simple/Rule_SampleRuleName46467274.java (2:500) : The import com.sample.event.rules.simple.TransformEvent.transformEvent cannot be resolved
com/sample/event/rules/simple/Rule_SampleRuleName46467274.java (7:1082) : The method transformEvent(TransformedEventCallBackHandler, EventTemplate, FirmwareEvent) is undefined for the type Rule_SampleRuleName46467274
The "static imports are only available if source level is 5.0" error suggests the drools compiler is setting a source level less than 5, but why would it do that? I've update my JAVA_HOME and my pom.xml. It should be picking up the Java source level from there, right?
Upgrade to Drools 6, this issue is fixed there. We had a similar issue for Java 8 recently with Drools 6.0 and it's fixed for 6.1 (and recent versions of 6.0 too).
The cause for java 8 was that drools recognizes java versions 1.6, 1.7, but not 1.8 at which point it defaults to 1.6. I suspect that drools 5.5 (which is old) had the same problem for java 7.
Turns out Drools uses the Eclipse JDT compiler to compile rules. Upgrading the JDT version did the trick. However if that fails, upgrading the Drools version might also help.

How to compile a JAR for Java 1.5

I'm using the PostToWeb library for Processing (http://libraries.seltar.org/postToWeb/), but when I try to run the sketch, I get an error telling me that the JAR for the class is compiled against Java 1.6, whereas the version of Processing that I'm using (1.5) uses Java 1.5
So, how would I go about recompiling the code src against Java 1.5?
Or, is there some other potential workaround?
Thanks.
If you are using a dev tool, you should be able to mention the compile version in the project properties.
in Eclipse, Project Properties, Java Compiler, set compliance level to 1.5.
It's as simple as:
javac -target 1.5
Otherwise you can specify it in maven with the compiler plugin
<compilerVersion>1.5</compilerVersion>
You could switch the version of Java you are running with to be version 6. Any jar compiled with version 5 will work with version 6. Just not vice versa. Then in the end you have a system running with an updated Java.
There are command line parameters for the compiler that can control this. I have done this with Ant.
I googled for you and found http://docs.oracle.com/javase/1.5.0/docs/tooldocs/windows/javac.html which tells you to use -source 1.5 and -target 1.5 for these compiles.

Will programs written against "Java 8" JDK be compatible with "Java 7" JREs?

My question is if Java JDK and JREs have to be compatible to run?
I mean: will Java applications written using JDK version 8 in future work with current JRE's?
It is possible to use cross-compilation options when compiling. Do that and it will be possible to compile code with SDK 8 that is compatible with Java 1.1. It won't be very advanced code for 1.1, but it will run.
The short answer is No.
If you develop your application in JDK 8 and run it with JRE 7, you would get an UnsupportedClassVersionError.
This question is two part:
JDK vs JRE
forward / backward compatibility.
JRE is the acronym for Java Runtime Environment. JDK is the acronym for Java Development Kit: a set of tools which you use to develop Java programs. The JDK also contains a full JRE. In general there is no compatibility issue between the two. But you might want to take care not to use libraries which are only available in the JDK (for example code generation or the tools.jar)
Java itself is compiling to bytecode, which is forward compatible. That means you can use bytecode of any Java version and run it with any newer version. The other way around generally doesn't work and is checked by using the class file version ("java.lang.UnsupportedClassVersionError: Test : Unsupported major.minor version 51.0").
Then there are Java libraries, including the core libraries. So far there was never anything removed from them, so they are forward compatible. This is probably going to change with Java 9 where a very small usually unused library functions are removed.
Regarding to backwards compatibility, this is possible by setting the Java compiler to produce Bytecode of an older version. Up until Java 8, the compiler was always able to produce bytecode of the last two major versions as well. However, you might successfully compile a Java 8 source to Java 6, but not be able to run it. That is the case when you use libraries that are only available on a never Java. For such cases there is for example the maven animalsniffer plugin which will verify that when you compile against an older version, you actually only use libraries existing in said version.

Categories

Resources