how to specify JRE to run a executable jar - java

I am shipping a executable jar file to customer, Customer has installed JRE 5, JRE 6 and JRE 7 on the box. My Jar required JRE 7 to run.
Without changing the system PATH (Environment var) how can I specify the JRE 7 to use?

You can specify full path to that JRE that you need, for example:
/path/to/jre/bin/java.exe -jar executable.jar
or
/path/to/jre/bin/javaw.exe -jar executable.jar
If you run this from a shell (script) then it is good practice to first set the JAVA_HOME environment variable to the right location before (/path/to/jre) before running the executable. You could first set/export JAVA_HOME and then extend it to the location of the Java executable (e.g. %JAVA_HOME%\bin\java.exe on Windows). More information here.

I'm not sure there's a cross-platform way to achieve this.
On Windows you can use a tool such as launch4j to wrap up the jar as a .exe that can select an appropriate JRE.
On Mac OS X you can have several different JDKs installed in parallel but only one public JRE (which will be at least the latest version out of the installed JDKs, and may be newer if it's been auto updated). It's the public JRE that is used for app bundles and when double clicking a JAR in finder.

The simplest way to do this, is to use Java WebStart to launch your program and then specify you need a suitably new version of Java. The launcher will then locate a suitable distribution on your system to use.
Using javaws also allows you to easily distribute new updates to your users.
Caveat: Caching has notoriously been a problem over the years. Ensure that when a jar changes content, its URL changes too.

Related

Best method to install an Oracle JRE without changing Default Java?

I want to install Oracle JRE 1.8.202 x64 without making any changes to the current default java on target machines.
Detail on what I mean by 'Default Java'. I mean I don't want the installer to change of any of the settings that any current or old Java installer may have made to make itself the default (e.g. path changes, java executables dumped in windows or system32 directories, java.exe added or updated to C:\ProgramData\Oracle\Java\javapath\java.exe, environment variables like JAVA.HOME, Java executables added to registry apppaths).
The target machines are a mix of Windows machines that will have a huge variety of current Java installations. Some apps in the target environment rely on using the default java to run - I'd rather they didn't, but that's out of my control.
I can't see any options to stop the installer (jre-8u202-windows-x64.exe) setting it's JRE to the default. See the installer switches at Oracle JavaSE 8 Docs.
Anyone know of any methods other then just copying the files into a target directory?
Snapshotting the current default then restoring it is not an option - Oracle have changed 'setting the default' technique over time, so restoring cleanly would be a fragile rabbit hole.
For both JDK and JRE installations, Oracle differentiates between "patch in place" and "static" installations, where the former refers to updating an existing JRE with newer components in an existing directory, and the latter refers to performing a standalone install.
You want to run the install via command line:
jre-8u251-windows-i586.exe f:\jre_config_file
where jre-8u251-windows-i586.exe is the latest version of JRE 8 (of course you can specify whatever flavor of JRE 8 you're actually after), f:\jre_config_file is a file containing the line STATIC=1 and any other config options you're after.
You could also just feed the installer STATIC=1 as a command line arg.
The STATIC option is actually listed in the page you linked, so I'll spare you additional blather about config.
I'm not sure if it's the best method but you could try using SDKMan to manage your Java versions on your target machine.
I did not find a managed way to use the installer without changing target system default java.
Instead, a custom installation process copies the installation tree into the target directory. Works fine.

Can we install two versions of Java JDK on Windows?

I have created an executable JAR file developed on Java version 8. The JAR file was opening on double click. But as the Oracle applications support only Java 6, I had to install JRE 6, but then after the JRE 6 installation, my executable JAR file is not opening.
I have set the JDK 8 bin path in Path environment variables. Is there a solution for this problem? Why is the JAR file not opening after two Java versions in the system?
JAR should open even if two versions 6 and 8 of Java are installed in the system.
You are facing a backward compatibility problem. Backwards compatibility means that you can run a Java 6 program on a Java 8 runtime, but not the other way around.
You can run a lower configuration on a higher configuration, not vice-versa
There are several reasons for that:
Bytecode is versioned and the JVM checks if it supports the version it finds in .class files.
Some language constructs cannot be expressed in previous versions of bytecode.
There are new classes and methods in newer JREs which won't work with older ones.
If you really, really want (tip: you don't), you can force the compiler to treat the source as one version of Java and emit bytecode for another, using something like this:
javac -source 1.8 -target 1.6 MyClass.java
You can compile your code to Java 1.6 bytecode using JDK 1.8. Just take care of the following:
-source=1.8 and -target=1.6 compiler options
If you use Maven, consider having two pom.xml files, with an optional parent file.
Source: Can program developed with Java 8 be run on Java 7?
I am not sure if this solution going to work or not.
Try to run command java -version and look if it returns java 6 or 8 path. Also try to give path of JDK 8 as JAVA_HOME variable and add that into path like this path=%JAVA_HOME%/bin and see if it works. If you get the java 6 as java version try to use above method and then install JRE 6
Hi All Thank you for your response. I kept java6 and reinstalled java8 and now forms and jar both are working!.
In the short term,
the answer is yes. Since both JDK files are downloaded as jar fils it will ok to download both jar files. The reason to not opening after two java versions is as #Elliott said: "in the system is Java 6 can't run Java 8 compiled code, you should be getting an error." That's exactly true but the problem is how to use multiple versions of JDK in a single machine.
Then we have to move on to long term,
The tricky thing is to manage these multiple JDKs and IDEs. It’s a piece of cake if I just use Eclipse for compiling my code because the IDE allows me to configure multiple versions of Java runtime. Unfortunately (or fortunately), I have to use the command line/shell to build my code. So, it is important that I have the right version of JDK present in the PATH and other related environment variables (such as JAVA_HOME).
Manually modifying the environment variables every time I want to switch between JDKs, isn’t a happy task. But, thanks to Windows Powershell, I’m able to write a script that can do the heavy lifting for me.
Basically, what you want to achieve is to set the PATH variable to add the Java bin folder and set the JAVA_HOME environment variable and then launch the correct Eclipse IDE. And, I want to do this with a single command. Let’s do it.
Open a Windows Powershell.
I prefer writing custom Windows scripts in my profile file so that it is available to run whenever I open the shell. To edit the profile, run this command: notepad.exe $profile - the $profile is a special variable that points to your profile file.
Write the below script in the profile file and save it.
function myIDE{ $env:Path = “C:vraajavajdk7bin;” $env:JAVA_HOME = “C:vraajavajdk7” C:vraaideeclipseeclipse set-location C:vraaworkspacemyproject play }
function officeIDE{
$env:Path = "C:vraajavajdk6bin;"
$env:JAVA_HOME = "C:vraajavajdk6"
C:officeeclipseeclipse
}
Close and restart the Powershell.
Now you can issue the command myIDE which will set the proper PATH and environment variables and then launch the Eclipse IDE.
As you can see, there are two functions with different configurations. Just call the function name that you want to launch from the Powershell command line (myIDE).
If any issue please put a comment below!

How to force a jar to run in java 7?

I made a Runnable Jar that will run as service in windows, but the final pc that will execute him, has 3 versions of java installed, 1.3, 1.6 and 1.7, and my jar needs to be executed with 1.7, because him uses classes that are present only in 1.7+ version, o don't know how are configured the environiment variables, my question is:
Is possible to force my code to run in java 1.7?
You can also do this without persistently resetting your path environments. Just use the absolute path to your jre/jdk instead of typing "java": e.g.: "C:\Program Files\Java\jre7\bin"\java -jar foo.jar, same goes for MacOS and Linux.
Set your JAVA_HOME and PATH environment variable to point to the Java 7 directory.
See https://superuser.com/questions/284342/what-are-path-and-other-environment-variables-and-how-can-i-set-or-use-them to know how to do it.
Rather than setting path/classpath at top level, you should create a
batch file and set path/classpath there.
Then command to run your jar.

Setting JAVA_HOME for intelliJ?

So i decided to try the beta of android studio today, but it refuses to run on my 32-bit JRE. I can download the 64-bit JRE, but im not interested in it being my default JRE for various reasons (my current 32-bit eclipse IDE, Processing IDE and Minecraft doesn't like 64-bit to my experiences), and i think that swithing java_home dir all the time will be cumbersome.
So does intelliJ have some way of setting an alternative JAVA_HOME dir like eclipse does in its ini?
In IntelliJ you can specify which SDK to use at project level. go to "File" --> "Project Structure" --> SDKs (list on the left) and you can add/remove paths to different SDKs
Other solution: If you use Windows then I think you can specify JAVA_HOME as system property just for IntelliJ. If you use Linux the solution is similar.
I use IDEA_JDK for this purpose (on linux).
I believe it is STUDIO_JDK for android studio.
For more information on this and other jetbrains IDE's, see https://intellij-support.jetbrains.com/hc/en-us/articles/206544879-Selecting-the-JDK-version-the-IDE-will-run-under
The other answers will not work for 64bit versions. Jetbrains have actually documented this quite well. From https://intellij-support.jetbrains.com/hc/en-us/articles/206544879-Selecting-the-JDK-version-the-IDE-will-run-under:
Java 8
Please be aware that Java 8 is required to run IntelliJ IDEA starting from version 16 on all the supported platforms. It also applies to the lightweight IDEs released from the same branch (144+).
Windows
JDK is bundled with all the product installers and it's recommended that you will use the provided JDK version unless there are any specific issues.
32-bit JDK is located in IDE_HOME\jre directory and is used by the 32-bit product executable.
To run the IDE in 64-bit mode you will need to download and install 64-bit JDK (not JRE) distribution and install it yourself. IDE will find and use it from the registry when you run the 64-bit .exe file (available only for IntelliJ IDEA right now, other products can use the .bat file to run in 64-bit mode).
<product>.exe uses this JDK search sequence:
IDEA_JDK / PHPSTORM_JDK / WEBIDE_JDK / PYCHARM_JDK / RUBYMINE_JDK / CLION_JDK / DATAGRIP_JDK environment variable (depends on the
product, WEBIDE_JDK applies to both WebStorm and PhpStorm before
version 2016.1)
..\jre directory
system Registry
JDK_HOME environment variable
JAVA_HOME environment variable
idea64.exe uses this JDK search sequence:
IDEA_JDK_64 environment variable
..\jre64 directory
system Registry
JDK_HOME environment variable
JAVA_HOME environment variable
It’s also possible to start the IDE with .bat file located in the bin directory, it uses the following JDK search sequence:
IDEA_JDK / PHPSTORM_JDK / WEBIDE_JDK / PYCHARM_JDK / RUBYMINE_JDK / CLION_JDK / DATAGRIP_JDK environment variable (depends on the product)
..\jre directory
JDK_HOME environment variable
JAVA_HOME environment variable
Environment variable must point to the JDK installation home directory, for example:
c:\Program Files (x86)\Java\jdk1.8.0_66
The actual JDK version used by the IDE can be verified in Help | About dialog (open any project to access the menu).
Define IDEA_JDK / PHPSTORM_JDK / WEBIDE_JDK / PYCHARM_JDK / RUBYMINE_JDK / CLION_JDK / DATAGRIP_JDK variable depending on the product to override the default version from IDE_HOME\jre.
Use Rapid Environment Editor to add/edit the variables, it will detect incorrect paths.
Linux
Starting from IntelliJ IDEA 16 and the most recent versions of the lightweight IDEs, we are bundling custom JRE with Linux distributions, just like we've been doing for Mac. Our custom JRE is based on OpenJDK and includes the most up to date fixes to provide better user experience on Linux (like font rendering improvements and HiDPI support).
Boot JDK path is stored in the .jdk file located in the config folder. It can be modified either via the Change IDE boot JDK action or by manually editing .jdk file (if you can't start the IDE to change it via an action).
It's recommended to use the bundled JRE (if available). In case you have any issues with the bundled version, you can switch to the latest version of Oracle JDK or OpenJDK available for your system (OpenJDK 1.6 is not supported, please use 1.7 or later versions, JDK 1.8 is recommended and older Java versions are not supported starting from IntelliJ IDEA 16).
Check bin/.sh file for the JDK search order, it's similar to Windows in terms of the environment variable names. It's a legacy way to adjust the boot jdk, use it for older product versions. Consider using .jdk file instead (see above), so that your modifications can survive IDE updates/re-installation.
Check this answer if you need to install the JDK manually on Linux.
If you have problems with ugly fonts, please see this thread comments for the tips.
Help | About will show the actual JDK version.
Mac OS X
Our latest IDE versions come with the bundled custom JDK 8 which contains the fixes for most known OpenJDK bugs. Should you need to use a different Java version, please refer to https://blog.jetbrains.com/idea/2015/05/intellij-idea-14-1-4-eap-141-1192-is-available/.
If you override IDE JDK version, its path is stored in .jdk file located in the config folder (idea.jdk for IntelliJ IDEA, pycharm.jdk for PyCharm, etc). Delete this file or change the path inside the file manually in case IDE no longer starts and you can't change it via the menu.
If IDE doesn't start and this file doesn't exist, create it manually and specify Java path to use (Java home location), for example:
/Library/Java/JavaVirtualMachines/jdk1.8.0_60.jdk
A reminder - once you set up a JAVA_HOME env variable in Windows, you need to close all your IntelliJ apps, and then start again. Otherwise, there might be troubles with accessing JAVA_HOME variable.

jdk installer for windows 7 does not set path

It seems the JDK installer on Windows 7 does not install a path variable to find javac. I can set it manually. But I do not like to change it with any new update of JDK. Does it use any other environment variable? Or is it really that weird?
I don't know of a JDK installer which sets the PATH on any system.
You can install several version of the JDK and if you use an IDE, you don't need to set the path. If you want to use javac manually (don't ask me why you would) you need to set the PATH to specific which version to use.
Yes You can using environment variables. Which available in Control panel -> system

Categories

Resources