Eclipse - use OS environment variable in build path - java

Say I have two Windows environment variables named JAVA8 and JAVA6
which point to the root folders of the respective JDKs.
Can I use the the JAVA8 Windows variable in Eclipse
(to e.g. add it the build path of a given project)?
I mean can Eclipse refer to %JAVA8% directly once it's set at the OS level?
Also, I want to make this project portable so that I can give
this project to another team member (who might be on Mac OS e.g.)
with the only requirement for him to create a JAVA8 environment
variable on his OS.
I did quite some searching on the web (incl. in SO), and this seems
impossible (but I doubt such a basic thing would be impossible).

Eclipse expects you to define the available JDK/JREs in 'Preferences > Java > Installed JREs'. In the project Java Build Path you reference this by name rather than path so that it is independent of the actual JDK/JRE location.
You can also use 'Preferences > Java > Installed JREs > Execution Environments' to choose a JRE for things like 'JavaSE-1.8'. Again you can use the Execution Environment name in the Java Build Path.

Related

Paths to multiple JDK's in windows system variable PATH

Imagine, in my Path variable on Windows 7 I have the path to my jdk version 8 already included. Now I start a new project where I am using Java 11 for example and I add the path to java 11 JDK's bin also to the Path variable. I am wondering, if it is required to remove the path to the (old) JDK 8 in this case from the Path variable for a correct working? If I leave it there, will the correct Java version be picked up for the new project? And also: In my IDE, when creating a new project, I also do select a JDK to use. What is this choice used for and what is the specification of the JDK in the path variable used for? I am not very familiar with operating systems, so please explain it :)
1) the system's path variable
The path variable defines where the system will search for executables you are using on the console/shell.
Having multiple JDKs in your operating system's path variable is a bad idea. One will take precedence and you cannot even (or should not) be sure which one it is.
If you need different JDKs for different projects, you may create a script setting the environment. Let's call it configure.bat for Windows. When opening a shell you would first run that script to set all environment variables and probably start needed services.
Use commands like java -version or mvn version to check the JDK you shell is using!
You may alternatively be able to create the environment settings by configuring an instance of your shell in a different way, but I unfortunately can't give you any details about that.
There is an alternative ...
2) the IDE's path variable
In you IDE (Eclipse, IntelliJ) you can also configure the JDK, and more important: you can configure different JDKs for specific code levels and can set the code level (or the JDK directly) on each of your projects individually. While working in the IDE you don't need to run configuration scripts, the IDE will take care of that. Be careful though, when using a system console in the IDE, you might be falling back to 1) then.
3) Maven
In a similar way as in the IDE you can configure:
JDKs (.m2/settings.xml)
code levels (the project's pom.xml or one of the parent POMs)
when using Maven. For more details see:
http://maven.apache.org/plugins/maven-compiler-plugin/examples/compile-using-different-jdk.html
This should be possible for other build management tools like Gradle. I have to refer you to a web search for those, though.

Update Maven Keeps Reverting Back to JavaSE -1.8 in Eclipse from Java JDK 1.8

How do I stop maven from constantly updating my Java System Library from JDK 1.8 back to Java SE 1.8? I have configured the build path and I even set my Java_Home Variable to the JDK path. I have also have updated the build configuration. Can someone please specify how to do this with some specific instructions as I am a novice. I also noticed this keeps changing back as well. FYI I am using Eclipse Mars if that matters.
In Regards to the comments below I have shared the Eclipse M2e Plugin screenshot. Even when selected I am not able to proceed to next. I also have shown what's already installed just in case another plugin is hindering me from using the m2e
You need to understand what an Execution Environment (EE) is in Eclipse. The concept of EE is an abstraction over JREs, allowing projects to be configured without absolute paths to JRE locations. From the wiki page:
Execution environments (EEs) are symbolic representations of JREs. For example, rather than talking about a specific JRE, with a specific name at a specific location on your disk, you can talk about the J2SE-1.4 execution environment. The system can then be configured to use a specific JRE to implement that execution environment.
In general, it's not advisable to configure a project to use "Workspace default" as its JRE System Library, because that makes the project inconsistent when loaded into different workspaces. Think about this: what if the project is being developed targeting Java 7, but I pull it into my workspace which has JDK 8; that could be a big problem. By using an EE, the project is configured such that it doesn't know (or care) where I actually have a matching JRE on my system.
I said all that to set up the answer for you, so you understand what Maven is doing and these instructions are doing. m2e, the Maven integration plugin for Eclipse, is (rightly) setting the project configuration to use an EE instead of "workspace default". From your screen shot I can see that you have both JDK and JRE 1.8 set up in your workspace, so ideally you should remove one (the JRE). Got to Preferences > Java > Installed JREs. There you'll see both the JRE and JDK listed. I suggest removing the JRE*.
Then go into the "Execution Environments" preferences section, select JavaSE-1.8 and make sure that your JDK is checked as the default implementation.
Now when m2e configures your projects to use an EE, that EE will be pointing to the JDK you have installed. And if someone else imports the project, it will point to wherever they have a matching JDK installed.
*By the way, it's perfectly acceptable and normal to have different versions of Java there; I often work on different projects that target different Java versions.

Installed two versions of Java, which one takes precedence?

I have to install a lower version of Java on some clients but some of them have a new version already installed. Do you know which one will take precedence? Is there a way to force Java to use a certain version without uninstalling the other? We have an application that requires the certain lower version.
By client I suppose you mean some Windows machine?
You can control which version is used by setting:
The system path to include the java.exe you want used at runtime.
The JAVA_HOME variable to point to the JDK library, only if development takes place on the machine.
If the client you are using does NOT have the option to setup JDK, then it will look into what ever JAVA_HOME you have defined in your OS environment variable.
There are clients, such as Eclipse, where you can define any version of JDK installed on your machine.
Also, if you are just launching your app using Java (and the app itself is not invoking any Java), you may be able to get away with just setting the shortcut that launches the app:
...\my\path\to\java\java.exe NameOfAppMainClassInYourJar.class
and setting the classpath as indicated here.

Need to use two Java versions in one machine

My application will support only java 1.6 version and I need to execute all test cases using java 1.7 compiled class files code.
My question how to reach above requirement...
Is it possible to install both java 1.6 and java 1.7 versions in my machine?
If possible how can i set java 1.6 path for my application and how i can set path for java 1.7 version to execute test cases using java 1.7 compile files.
Application will not open with Java 1.7, but I need to use java 1.7 compiled files only.
Yes , it is possible. You need to change JAVA_HOME environmental variable. You can set it in the script that is used to start the application.
This article might help you
Yes it is possible.
You can install multiple java versions in single system.
To make it simple. You can use eclipse , or Netbeans IDE. In that you can use different java versions to compile.
Like any program, you need to specify the directory of the program you want to run, either directly by giving the full path name, or indirectly by setting an environment variable like PATH or JAVA_HOME OR JDK_HOME
You can install any number of updates/versions of Java until you run out of disk space. To configure each program to use a specific version you need to look at how they are started and set appropriate directory to use Java from for that program.

issue with java and environment variables on windows

I've got a curious problem. After installing java jdk 6u25 I tested the installation by typing java in the command prompt. Everything worked fine , the console displayed the usage options as expected. This was about 3 weeks ago - I recently reinstalled windows 7 on my machine- Now I would like to get started with the play framework and followed a tutorial. I got a new application with play new but couldn't use play run because of this:
play! 1.2.3, http://www.playframework.org
Ctrl+C to stop
Could not execute the java executable, please make sure the JAVA_HOME environment variable is set properly .
The funny thing is that typing java in the console still displays usage options and neither eclipse or netbeans complained about a missing jdk. Nevertheless I checked my environment variables and was surprised to find:
No reference to java at all.
Seriously I got no idea how the console, eclipse and netbeans found my java. As a first countermeasure I once again reinstalled java hoping that the installer might set the path correctly. It didn't. Now I would like your advise: Should I just create the JAVA_HOME variable ? If I do, will further versions of the jdk override the variable to point to the new version or will I have to change the variable manually everytime? What about eclipse and netbeans. I would guess that they have been downloaded with their "own" jdk. If I want to update java on my machine, will it update their versions, too ?
Update:
I set the environment variable manually and restarted the pc. Now everything works fine but I would like to know what symbolic link is. Could you just give me a link in a comment or explain briefly ?
Eclipse and netbeans use a different way of finding your java installation than command line tools likes play, ant, maven, gradle etc.
IDEs may search your registry, guess default locations, or even have the JRE prepackaged with the installation.
Command-line tools usually rely on JAVA_HOME variable. And you have to set that manually.
Should I just create the JAVA_HOME variable ?
There are several tools that use that environment variable, so I'd recommend creating it.
If I do, will further versions of the jdk override the variable to point to the new version or will I have to change the variable manually everytime?
If you install your new Java version into the same folder (or if you create symbolic links), then no.
What about eclipse and netbeans. I would guess that they have been downloaded with their "own" jdk. If I want to update java on my machine, will it update their versions, too ?
That depends on whether the JDK is prebundled and how the configuration is done. I can't speak for Netbeans but in Eclipse, you could add this to your eclipse.ini:
-vm
C:/Java/jre/bin
Note that this is an example path to the bin folder. If you don't add versions there or use symbolic links (see above) then you'd not have to update the path when installing a new version.
It is possible that you have a (older?) java.exe in C:\windows or other common directory.
Make sure that the java you get on the command line is the one you think it is:
java -version

Categories

Resources