IntelliJ IDEA 2016.3 unable to compile with java 7 - java

I've recently updated to IntelliJ IDEA 2016.3 and I just noticed that whenever I have a project with a java 7 SDK selected, IntelliJ compiles the project with java 1.8.0_112-release (which must be a compiler bundled with IntelliJ because I have never installed it myself).
If I select a java 8 SDK other than the 1.8.0_112-release, IntelliJ compiles with that compiler.
If I have a java 7 SDK selected and run a main method in my project, IntelliJ uses that java 7 SDK version to run so the issues seems to only relate to the compiler.
I have tried re-installing all my java installations.
When I change the SDK I usually only change Project Structure -> Project -> Project SDK, but I also verify that the Language level is correct and that Project Structure -> Modules -> Dependencies -> Module SDK is correct.
All settings are default under:
Settings -> Build, Execution, Deployment -> Compiler/Java Compiler
It seems to me that IntelliJ IDEA 2016.3 is not able to use a java compiler lower than java 8, but my googling attempts have not been able to confirm this.
I can also mention that the only thing I haven't tried yet is a hard uninstall of IntelliJ where all my settings are wiped as well. A silent uninstall have been tried.
If I try to compile a new Hello World project with a Java 7 SDK this is the output in the Messages tab: Information:Using javac 1.8.0_112-release to compile java sources. If however I set the project SDK to a Java 8 SDK this is the output in the Messages tab: Information:Using javac 1.8.0_45 to compile java sources.
Have tried setting Settings -> Build, Execution, Deployment -> Compiler -> Java Compiler -> Project bytecode version to 1.7 without any effect.
File -> Other Settings -> Default Project Structure -> Project SDK and Project language level set to the Java 7 SDK. Message tab still displays: Information:Using javac 1.8.0_112-release to compile java sources
Has anyone else experienced this and know what causes this behaviour?

You need to set 2 things:
CTRL + ALT + SHIFT + S --> Project --> Project SDK
CTRL + SHIFT + A --> Type "Java Compiler" --> Target Bytecode Version

Apparantley this was caused by an issue in IntelliJ as stated in this
issue.
It has been resolved as of IntelliJ IDEA version 2016.3.1 and I copy-paste the comment from Eugene Zhuravlev in the youtrack issue to explain how it has been fixed:
A new compiler option has been introduced for javac: File | Settings | Build, Execution, Deployment | Compiler | Java Compiler | "Use compiler from module target JDK when possible". The default value is "on".
When this property is enabled and JDK associated with the module differs from the build's own JDK, the build will prefer compiling with the javac from the associated JDK rather than the compiler from the JDK it runs on.

there is a distiction between Project Structure and Default Project Structure (the latter is for newly created/checked out Projects).
Default Project Structure hides in File -> Other Settings.
There you would have Project SDK and Project Language Level.

Related

Intellij insists on compiling with javac 11.0.3 instead of Java 7

I have a project with many modules, all of which need to be compiled using Java 7. I have made sure every setting that I know of to point to that version of Java, yet somehow, every time I rebuild the project I get:
Information:javac 11.0.3 was used to compile java sources
followed by a list of errors:
Warning:(86, 25) java: as of release 9, '_' is a keyword, and may not be used as an identifier
Error:(86, 25) java: cannot find symbol
symbol: class _
location: class com.(...)`
Following other questions like:
Can't resolve -source errors on IntelliJ with regards to Project Language Level
IntelliJ IDEA tells me "Error:java: Compilation failed: internal java compiler error idea"
I have tried these settings:
Under Project Structure -> Project -> Project SDK: 1.7
Project Structure -> Project language level -> 7 - Diamonds, ARM, multi-catch etc.
Under Project Structure -> Modules checked every module, and that was not fun, because there are about 50. Sources tab -> Language level: Project detaulf (7 - Diamonds, ARM, multi-catch etc.)
Project Structure -> Modules, each Dependencies tab -> Module SDK: Project SDK (1.7)
Project Structure -> SDKs: it listed IDEA's built in Java 11, so I removed it, leaving only: 1.7
Settings -> Java Compiler -> Use compiler: Javac
Settings -> Java Compiler -> Use compiler from module target JDK when possible: unticked
Settings -> Java Compiler -> Per-module bytecode version: all modules set to 1.7
I am running the latest version of IntelliJ IDEA:
IntelliJ IDEA 2019.2 (Ultimate Edition) Build #IU-192.5728.98, built on July 23, 2019
Is this a bug with IDEA?
Here is the reason Intellij is choosing java 11:
https://www.jetbrains.com/help/idea/configuring-build-jdk.html
IntelliJ IDEA checks all JDKs that are used in the project: the JDKs
that are defined on both the project and module levels.
IntelliJ IDEA calculates the latest of these JDKs. This is necessary
to make sure that all modules can be compiled.
If the version of the latest JDK configured is lower than 1.6,
IntelliJ IDEA will pick the JDK version that is used for running the
IDE. This limitation is related to the fact that the compiler API used
by IntelliJ IDEA for building projects is supported starting from JDK
1.6.
And that is why switching the boot JDK to JDK8 re-enables the compilation of older java classes.
As mentioned in the comments by #Arnaud Claudel, I have tried switching the boot JDK of IDEA (described here https://www.jetbrains.com/help/idea/switching-boot-jdk.html) which helped. Incidentally, the lowest JDK version that IDEA will accept is 8.
I am not sure it is actual solution though and not just a workaround, because why would the IDE use JDK 11 in the first place, ignoring all the project's settings.
So I am leaving this question open for anyone to post a better solution.

Eclipse setup for a java project

Have a java project with couple of main java classes in it. On server this program has to run using java 1.5 version.
Trying to setup the same in eclipse.
Project properties/Java compiler ---- set to 1.5
Project properties/Java build path ---- Added JRE library ---> J2SE-1.5
But, when running one of the main class in the project in eclipse getting error as
SEVERE: Java Runtime Environment (JRE) version 1.6 is not supported by this driver
what else do I need to do in eclipse to run the program using java 1.5??
Go to Window -> Preferences -> Java -> Compiler and choose the default compliance level as 1.5
In Project properties/Java compiler make sure that 'Enable project specific settings' is checked.
Also, I would double check the build path for duplicate JRE libraries

intellij feature (...) not supported at this language level. I can't compile

IntelliJ is showing me this error
I just built my new computer and none of my java stuff is working... My regular Intellij was working when I first tried it, but my android studio would't install because it couldn't find the path to the jdk. Here is the question that I asked about that. Since that wasn't working, I had to reinstall java multiple times, and now my IntelliJ isn't working. Here is a picture of the error (I am getting an error that is telling me the for-each loops aren't supported at this language level).
I have tried to install the JDK on my D: drive and that didn't work. I am currently trying to use java 1.8u71 (have tried u65 and 1.7u47) except none of those have worked.
IntelliJ is showing me this error
Matching the SDK/JDK with Project language level
To activate Java language features in the editor, you need to change your module's Project Structure or Module Settings to ensure your Project SDK matches the Project language level.
Project SDK (JDK) Java 1.5 matches project language level 5 ...
Java 1.6 matches language level 6 ...
Java 1.7 matches language level 7, and so on.
Example
Depending on your IntelliJ version, hit F4 or Ctrl + Alt + Shift - S on your module in the Project pane, or hit Ctrl + Shift - A and type either "Project Structure" or "Module Settings" to open the Project Structure dialog.
Under Project Settings section, click on the first item in the list, Project.
In the screenshot below, the Project SDK 1.8 (java version '1.8.0_72') matches the selected language level SDK Default (8 - Lambdas, type annotations etc.) because Java 1.8 matches SDK level 8. This activates the Java 8 language features for use in the editor.
If you don't have the JDK set up under Project SDK that is a different problem. Solve that one first by clicking New and adding a JDK.
When I had multiple independent modules in the project, I had to do below setting in addition to #activedecay's answer:
Under Project Settings, click on Modules. Select specific module. Select appropriate Language Level.
I had this same problem and none of the solutions I have found worked.
I was using Maven projects and so decided to try running a maven install. It too said:
[ERROR] thefile.java:[24,77] diamond operator is not supported in -source 1.5
[ERROR] (use -source 7 or higher to enable diamond operator)
[ERROR] theFile.java:[91,62] lambda expressions are not supported in -source 1.5
[ERROR] (use -source 8 or higher to enable lambda expressions)
So I added the properties to the Maven build pom.xml file:
<properties>
<spring.version>4.1.6.RELEASE</spring.version>
<java.version>1.8</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
</properties>
And refreshed the Maven project in IntelliJ. It worked!
I also had the same problem.
While I was accessing a static method of an interface through the name of the interface, I got the error saying
static interface method invocations are not supported at this language level 5
As this is Java 1.8, I have checked some things:
I am using Intellij. So by doing CTRL+ALT+SHIFT+S Project Structure dialog box opens, go to
Project -> Project language level
set this to SDK default (8-Lambdas,type annotations etc).
In Project structure open
Modules -> Sources -> Language level
set this to Project default (8-Lambdas,type annotations etc).
Apply-> OK.
Then the error will go away.
Let me tell the solution in one image.
File -> Project Structure -> Modules -> Properties tab -> Source Compatibility (Java 8)
Another issue I had that caused this error in a Gradle project was the sourceCompatibility setting in the build.gradle file. This must also match the value you are expecting to need. The default value that I have seen IntelliJ use when creating a new Gradle project is 1.5. If you are using lambdas, for instance, this will cause the build to fail.

Set Intellij Idea Compiler Language Level to 1.3

I'm using Intellij Idea 15.0.1 and have a Java project, it imports class of default package from the class of a named package.
Since it is allowed only in Java 1.3, I've set JDK 1.3 everywhere:
- Project Settings
- IDE Settings - Java Compiler
- Edit Configurations (Application)
But still when I make project it stops with "Cannot resolve symbol" error. Also making starts with the message:
Information:Using javac 1.8.0_51 to compile java sources
Why Idea still uses javac 1.8 and how to set java 1.3 for the whole project, including syntax highlight?
You need to install JDK 1.3, configure it in Project Structure | SDKs and select it as the project SDK in Project Structure | Project.

IntelliJ error - java: try-with-resources is not supported in -source 1.6 error. Even though in project settings 1.7 JDK is selected

Under File -> Project Structure -> SDKs i have 1.7 jdk but i still get this error:
added.java: try-with-resources is not supported in -source 1.6
(use -source 7 or higher to enable try-with-resources)
I have restarted IDE but to no avail
Newer JVMs allow you to compile your code using compiler versions of lower JVM versions. For example, JDK 1.7 will allow you to compile your code using the 1.5 compiler.
See the "Cross compilation section" here http://docs.oracle.com/javase/7/docs/technotes/tools/windows/javac.html
What you need to do in Intellij, is to go to File -> Project Structure -> Project and select the "Project SDK" as 1.7 and the "Project Language Level" as 7.0
Had to click on the 'error bulb' icon (or Alt+Enter) and select option to enable load multi-catch, try with resources, diamond ... and then reload and restart IDE.
IntelliJ IDEA allows to set the language level on a project basis as well as on on module basis. If you have set the language level to Java 7 or higher on the project level, and are still getting this error message, right click on the module, select "Open module settings". Make sure the language level for the module is set to "Project default", or if for one reason, you want to set it specifically, it should be at least Java 7.
I had the same problem with IntelliJ 13. You have to set the language level, either for the entire project or individual modules, on the "General Settings for Project" dialog. See IntelliJ IDE gives error when using Try-Catch with Resources for the complete answer.
If the above answers didn't help, are you using Groovy? Apparently Groovy doesn't support Java's try-with-resources in the current version.

Categories

Resources