I am compiling my web app in Netbeans against Java EE 5. I know that the String.isEmpty() function is only supported in Java 6. Having said that, I can still compile my project using the .isEmpty() in my code.
How come Netbeans is allowing my web app to compile if I am compiling against Java EE 5?
The compiler level is not equal to the JDK level you use for compiling. The compiler level only checks for the syntax and of course creates a different output. But the compiler itself will use the jdk on your classpath so if you compiled with java 5 option but with the java 6 jars on your classpath the code will compile without an error.
You should check your classpath.
As many have pointed out, Java EE versions are not strictly tied with Java SE (JDK) versions. Mostly, they require a minimal Java SE version but are compatible with later versions.
Java EE 5 specification (downloadable PDF here) says:
This specification requires that containers provide a Java Compatible™ runtime
environment, as defined by the Java 2 Platform, Standard Edition, v5.0 specification
(J2SE)
Since JSE versions are backwards compatible, you can take a container compatible with Java 5 and run it on top of Java SE 6 or Java SE 7.
You can check the compatibility level that Netbeans is using by checking the project "Properties > Source > Source/Binary Format"
If you still have doubts about "Java vs Java EE" you can look for several questions here on SO about the difference between Java SE and Java EE.
Related
I'm migrating multiple projects from Java 8 to Java 11 and I was wondering if Java 8 is forward compatible with Java 11.
In other words, is it possible to use artifacts compiled against Java 11 in Java 8 projects?
No, you cannot execute Java 11 bytecode on an older (any older) JVM. This has always been the case. Each new major compiler release has a new bytecode format that won't execute on older runtimes.
You CAN however execute bytecode from an older version on a newer JVM, so executing Java 8 bytecode on a Java 11 runtime is quite possible and you can thus use Java 8 (or older) compiled libraries in Java 11 projects, if the dependency requirements are met.
Do however keep in mind that Java 9+ removed a number of packages from the core libraries that were in there with Java 8, so you may need to supply new dependencies from third parties to replace those. Most notable of those (but certainly not the only ones) are XML parsers.
You can run projects that were compiled for Java 8 in Java 11 runtime.
You can run projects that were compiled for Java 11 in Java 8 runtime if you properly set the --target during compilation to target JDK 8 or less. This will of course also limit the set of features supported in the source code.
Generally, earlier versions of JDK are supported on later runtimes (i.e. Java 11 should run on Java 17), but there are some caveats because some of the features might get deprecated or changed. Always read the release notes and test before you upgrade.
From the docs
-source release Specifies the version of source code accepted.
If I have a JDK version , say , 1.8, and I mention -source=1.6 , what does it mean ? Does this only mean that whatever code I have written can be compiled by javac of JDK 1.6 or above ?
If that be case , why pass -source=1.6 during javac command ? As this will generate .class files and hence there is no source code left to mark (the source code compatibility to 1.6 or above) ? After javac command, all we get is the bytecode and no .java files.
Does this only mean that whatever code I have written can be compiled by javac of JDK 1.6 or above ?
Nope.
The -source=1.6 option means that your code can only use Java language constructs that are part of the Java 6 and earlier versions of the Java language.
For example, any Java 8 lambdas, or Java 9 var declarations would be flagged as compilation errors.
Java8 introduced lambda expressions. If you compile your application with -source=1.6 the compiler will not allow lambda expressions despite it being supported with JDK8.
If I have a JDK version , say , 1.8, and I mention -source=1.6 , what does it mean ? Does this only mean that whatever code I have written can be compiled by javac of JDK 1.6 or above ?
If your code uses Java 8 features, it won't even compile with -source=1.6. Otherwise, not necessarily true, but generally, yes, it should work with Java 6 and above.
If that be case , why pass -source=1.6 during javac command ? As this will generate .class files and hence there is no source code left to mark (the source code compatibility to 1.6 or above) ? After javac command, all we get is the bytecode and no .java files.
Javac is the Java Compiler. Of course it will generate .class files, as that is the compiled form of a java program. Why would you pass it? Let's say you want to target a specific version, this is the easiest way to keep support at that level.
You can test this by using a Java 8 feature ( lambdas, streams, datetimeformatter ), then try to compile. Your compilation will fail.
By specifying the source argument on the compiler, you are telling the compiler that you want the source code you are submitting to comply with that version of Java and check against specific language features for the version you selected (The default is the newest version typically, even if you don't specify source version yourself). The docs are clear on what values are acceptable and what value is the default. This does not change your source code or transform your code to older versions, it merely alerts you if you are using features that are in later versions of Java. If you are not using newer features of the java language then this will simply compile your code and generate class files as usual.
javac MyProgram.java -source 1.6
The command above will tell the compiler to treat the source code as it was compatible with Java version 1.6.
Below are the allowable values for JDK 1.8 and the description from the docs.
1.3 The compiler does not support assertions, generics, or other language features introduced after Java SE 1.3.
1.4 The compiler accepts code containing assertions, which were introduced in Java SE 1.4.
1.5 The compiler accepts code containing generics and other language features introduced in Java SE 5.
5 Synonym for 1.5.
1.6 No language changes were introduced in Java SE 6. However, encoding errors in source files are now reported as errors instead of warnings as in earlier releases of Java Platform, Standard Edition.
6 Synonym for 1.6.
1.7 The compiler accepts code with features introduced in Java SE 7.
7 Synonym for 1.7.
1.8 This is the default value. The compiler accepts code with features introduced in Java SE 8.
8 Synonym for 1.8.
I installed java 8 on Lubuntu 13.10. It is pre release but it should work - JDK™ 8 Early Access Releases.
I added JDk to eclipse:
And tried wrote first java 8 version code.
This code should find the longest word.
I caught a bunch of exceptions:
for 12 line - it shows:
Multiple markers at this line
- Syntax error, parameterized types are only available if source level is 1.5 or
greater
- Resource specification not allowed here for source level below 1.7
Why does this exactly happen?
How to solve this trouble?
Eclipse JDT environment -- editors and tools -- only supports Java Syntax up to 1.7. As of today, Java 1.8 support is only available in early access releases.
Adding a JDK or JRE to Eclipse does no change the characteristics of the JDT environment, which is a highly-integrated set of tools. So, until Eclipse releases Java 1.8 support and that makes its way into your distribution, you will not be able to use the Java editor for any syntax that is unique to Java 1.8.
I am switching an enterprise application built on Spring 3.1.0, Hibernate 3.6.0 from Java 6 32bit to Java 7 64 bit.
Has anybody done that? Any problems? Are there any resources on the subject?
Here are the pitfalls I know about:
You might have to upgrade your IDE to be able to enable Java 7 features.
Make sure that command line tools and your IDE use the same Java version. If you're using Maven, for example, look into $HOME/.mavenrc and/or check the environment variable JAVA_HOME
Check the source/target options of the Java compiler.
Java 7 supports generics better than Java 6 so some code will now compile that failed with Java 6
The 64bit version of Java uses a lot more memory than the 32bit version
The first release of Java 7 had a severe bug in the JIT compiler which broke Lucene. Use at least b2 or better.
Java 7 uses the newer JAXB 2.2 instead of 2.1 (the full change on the XML stack is described here). These versions are not compatible on generated code for Boolean getters and setters!
I am very new to programming and I am using Java as my first Programming Language. I am already using Java with Java SE for almost a week and I read something about Java EE and if I understood it correctly, Java EE = Java SE + other features, so I think I have to change my SDK to Java EE to save disk space.
I read the installation instructions about Java EE and it says there that its OK to have them both but I really wanted to have just Java EE. I uninstalled Java SE and installed Java EE but I was having some errors along the way and I can't continue installing Java EE. I can't remember what the errors were but I decided to cancel the installation. I installed Java SE again and installed Java EE and everything went smoothly. Now, I have both...
I am using JCreator 5 as my IDE and I configured it to use the Java EE jdk only. I tried to compile my test program but it seems that it cannot recognize it. How can I make my Java EE work without having Java SE.
I don't know whats going on. I am totally new to this. I just wanted to use J2EE... Please help.
Java SE is in C:\Program Files\Java while Java EE is in C:\Sun\AppServer
Java EE is defined by its specification and its specification is built on top on the Java SE classes . Java EE classes will call Java SE classes , so you cannot use the Java EE without the installation of Java SE . Otherwise , you will encounter the java.lang.NoClassDefFoundError
Java EE classes are defined over Java SE classes, so you need to install Java SE, first.
Happy learning!