Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I change the Java version I'm using in IntelliJ (and Maven)?

I'm working on a simple Java project and trying to use IntelliJ 14 for the first time; I have the Ultimate version through an education license (in my last year of undergrad!). I'm not super familiar with either Maven or IntelliJ. I want to use Java 8, and in fact I'm pretty sure this is the only version of the JDK I have installed on my computer. However, whenever I compile my project, I get a couple warnings and an info message:
Information: Using javac 1.8.0_51 to compile java sources
Warning: java: source value 1.5 is obsolete and will be removed in a future release
Warning: java: target value 1.5 is obsolete and will be removed in a future release

So what I've noticed is that under IntelliJ IDEA > Preferences... > Build, Execution, Deployment > Compiler > Java Compiler, the target bytecode version is set to 1.5. I can change it to 1.8, but when I quit IntelliJ and re-open the project, it is reset to 1.5. The project bytecode version is blank for SDK default.
Here's what I've already done:

  • included the following in my maven pom

    <properties>
      <maven.compiler.source>1.8</maven.compiler.source>
      <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
    
  • Changed the IntelliJ preference file as noted here on the IntelliJ website.

  • Set the global, project, and module SDK as noted here on the IntelliJ website.

Am I missing a setting somewhere? What do I have to do to keep this value from changing every time I re-open IntelliJ?

like image 703
Maribeth Avatar asked Jul 25 '15 23:07

Maribeth


2 Answers

I'm using IntelliJ IDEA 2017.2. To build the project (using Ctrl+F9), changing the maven-compiler-plugin source and target versions was not enough. I had to:

  1. Open File | Project Structure. (Or right-click a directory and select "Open Module Settings" to open the "Project Structure" dialog box.)
  2. Select Project Settings | Modules on the left side.
  3. For each module of my project (middle pane in the dialog box), change the "Language Level" on the right-side pane (Sources tab).

Then I ran into this other problem. To solve it, I had to:

  1. Open File | Settings.
  2. Type Compiler in the search box and select "Java Compiler".
  3. On the right-hand side, bump up the "Target bytecode version" for each module of my project.

Aside: I've been using IDEs since Turbo Pascal 2.0 in the 80s. I find it counter-intuitive that such a modern IDE makes it so hard to open the "Project Settings". First, if you right-click the project (top left on the main window) there's no option to open project settings. Second, the "Project" bar on the top left of the main window has the gear symbol with a drop-down list, but no option to access project settings there either. Third, if you right-click the project directory in the project explorer view on the left, you see "Open Module Settings" but there's no "Project Settings". Fourth, if you open File on the main menu, you'll see "Settings", "Other Settings" but no "Project Settings". Counter-intuitive. :^|

like image 43
Paulo Merson Avatar answered Sep 29 '22 19:09

Paulo Merson


Try using the maven compiler plugin in your pom.xml and set the source/target level accordingly. IDEA will change the project settings according to this setting

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
      <version>3.1</version>
      <configuration>
        <source>1.8</source>
        <target>1.8</target>
      </configuration>
    </plugin>
  </plugins>
</build>
like image 85
Gerald Mücke Avatar answered Sep 29 '22 19:09

Gerald Mücke