giotto
giotto

Reputation: 622

IntelliJ terminal jdk setting

I have a project in IntelliJ Idea that uses jdk 8. In my system I have many jdk installed for development. Every time I create a new terminal window inside IntelliJ, the latest system jdk (jdk 10) is used and not the project one (jdk 8 in this case) and I have to switch manually. Of course I realise the error only after some build which requires jdk 8 does not work properly.

Is there a possibility to set the jdk used in IntelliJ terminal?

Upvotes: 1

Views: 4059

Answers (5)

jmwdev
jmwdev

Reputation: 1

As per other answers - doesn't look like its supported but in the meantime I thought I would post a workaround that solves the poster's problem:

my solution is to create a shell script that launches with the terminal profile:

  • check the existence of .idea directory in current working directory
  • if it does then pull the configured name of the jdk from the misc.xml
  • find the jdk directory under $home/.jdks where intellij installs them.
  • add the found jdk bin directory to the head of the path in the current session

my solution is in powershell as I found it an interesting exercise to solve. The same can be achieved in Unix as easily if not easier.

# $Home\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1

$ideaDir = "./.idea"
if (Test-Path -Path $ideaDir)
{
    $jdksDir = "$Home\.jdks"
    $match = Select-String -Path "$ideaDir/misc.xml" -Pattern "project-jdk-name=`"(.*?)`" "
    $jdkName = $match.Matches.groups[1].value

    [array]$jdks = ls -Directory -Name $jdksDir
    $success = 0
    foreach( $jdk in $jdks) {
        if($jdk.Contains( $jdkName )) {
            $pathBlock = "$jdksDir\$jdk\bin"
            echo "adding $pathBlock to path"
            $env:Path = "$pathBlock;" + $env:Path
            $success = 1
            break
        }
    }
    if( $success -eq 0) {
        "failed to add jdk $jdkName to path"
    }
}

Some assumptions have been made here:

  • jdks are installed via intellij and jdk names are defaulted (I tested with correto-18 and openjdk-21).
  • I noticed is that workspace files are saved on exit, not on the change itself. If you're swapping around quickly then it's not much of a solution. If however you've got different projects on different jdks then it may make life easier. You can paste the code into your powershell profile to execute on new terminal sessions

Upvotes: 0

HellishHeat
HellishHeat

Reputation: 2481

For anyone else who stumbles on this question, here is how I resolved a similar issue:

Maybe this is just my company's security*, but when a JDK is installed with an .exe, something like the following path is created and added to the root of the %path% variable: C:\Program Files\Common Files\Oracle\Java\javapath

On that path you'll find a 'java' executable for the installed version of java.

The fix was to simply uninstall that version of java, via the system settings.

* I mean that I was unable to see the 'true' path variable, may have been my company's security.

Upvotes: 0

giotto
giotto

Reputation: 622

I repost the answer of @yole from the comments. It is not possible.

Upvotes: 0

Frito
Frito

Reputation: 446

Check Preferences->Tools->Terminal do determine the used shell implementation.

Change the shell path or change your environment settings with the used shell to take effect e.g. for setting JAVA_HOME and PATH.

You can even use the "shell integration" flag to load a custom rc config file. Please check the IntelliJ documentation for this.

You are not able to use the configured projects JDK in a dynamic way. If you need this, please file an enhancement request to Jetbrains.

Upvotes: 1

nilopanda
nilopanda

Reputation: 1

You need to change the SDK version in project structure. You can change it through IntelliJ navigation top bar, by following this path:

File > Project Structure > Project tab > Project SDK

Upvotes: 0

Related Questions