Set the jruby jvm without changing the JAVA_HOME - java

Is it possible to change the jre used by jruby without changing the JAVA_HOME environment variable in Windows?
I am running jruby with the jruby.exe but couldn't find any parameter for the jre path. I don't want to change my JAVA_HOME (which is Java 1.6) because it will affect other applications on my machine, but I just want to specify to Jruby to use the Java 1.7 jre otherwise I am getting errors.
I am using jruby 1.7.23.

if you set JAVA_HOME in the cmd line or "hard-code" it in a jruby.bat launcher (discouraged but if you must ...) that would only change it for the process run from that particular cmd line.

Related

Confused about what jdk and jre my command prompt is using for java

I was super confused about compatibility on a java tool I was using and ended up downloading different versions of java so now I do not know what versions of jdk or jre I am actually using. When I compile I use -- release 8 in my command prompt, what does that exactly mean?
Currently my programs folder has jdk-18 in the java folder
and my program files(x86) folder has
jdk1.6.0_45
jdk 1.7.0_80
jre 1.8.0_ 321
jre6
jre7
My java home environment variable is C:\Program Files\Java\jdk-18
My classpath has the file C:\Program Files (x86)\Java\jdk1.7.0_80\lib\tools.jar
when I type java -version in my command prompt I get
java version "1.8.0_321"
Java(TM) SE Runtime Environment (build 1.8.0_321-b07)
Java HotSpot(TM) Client VM (build 25.321-b07, mixed mode)
So does that mean my cmd uses jre 1.8.0_ 321? What about the jdk? How do I change it if I need to?
When you run java -version - first java found in PATH environment variable is used. To be sure use commands where java (Windows) or which java (Linux) - it will tell you the location.
When you run mvn install - JDK found in JAVA_HOME environment variable is used. To be sure use mvn -version - it will tell you the location.
When you run from IDE - IDE settings matter, usually you specify JDK per project.
When you run javac --release 8 Something.java you are asking compiler to produce output compatible with the version you specified - it has nothing to do with JDK you are actually running it on. This flag was added in JDK 9, so if it doesn't fail for you then it means you are running on JDK>=9.
If you want to be 100% sure just fully qualify the path - for example on Windows "C:\Program Files\Java\jdk1.8.0_152\bin\javac.exe" Something.java
So does that mean my cmd uses jre 1.8.0_ 321?
Yes.
What about the jdk?
Unclear.
Run javac -version and that will tell you what version compiler you will run by default.
The versions of the Java tools that you get when you type java, javac, jar (etcetera) at the CMD prompt are solely determined by the PATH environment variable setting in effect at the command prompt.
The other variables do other things ... but not this.
How do I change it if I need to?
If you want to change what typing java does, change PATH.
Note that the PATH variable is a standard Windows environment variable that affects all* commands, not just the Java tools. There should be lots of tutorials on how to set PATH, and on the Windows CMD prompt in general. You should probably take the time to read them.
You can also just use the absolute path for the Java tools; e.g. "C:\Program Files (x86)\Java\jdk1.7.0_80\bin\java.exe" is probably the correct path for the Java 7 java command. (You can easily check ... and find the correct path by looking at the installation tree.)
Regarding your other variables:
My java home environment variable is C:\Program Files\Java\jdk-18
That is presumably JAVA_HOME. That tells (many) 3rd-party tools which Java tools to use. But unless you have configured PATH to depend on JAVA_HOME, it won't alter what typing java does.
My classpath has the file C:\Program Files (x86)\Java\jdk1.7.0_80\lib\tools.jar
That is presumably CLASSPATH. That variable provides a default path that java will use to find classes to load. For example, if you run java com.acme.Example, it will use CLASSPATH to search for the compiled Example class.
So "C:\Program Files (x86)\Java\jdk1.7.0_80\lib\tools.jar" is almost certainly incorrect. (That JAR file contains the classes that implement various Java tools. It doesn't need to be on the classpath, unless your Java application is running one of those tools. And even then there would need to be other things on the classpath for your application to work.
You need to do some reading on what the Classpath is, how it works, and how to set it correctly. Setting it to stuff randomly is a sure fire way to make your Java code not work ...

Setting Environment variable in Linux

I am using Maven for my project and I need to set environment variables in my system for it to work. I have set environment variable for maven but it also requires setting up Java environment variable. I am accessing linux through Putty through a remote server. I tried setting up JAVA_HOME environment variable but I get this error:
Error: JAVA_HOME is not defined correctly.We cannot execute /home/z222189/jdk1.8.0_31/jre/bin/java
I used export JAVA_HOME=/home/z222189/jdk1.8.0_31/jre for this purpose
Even when i try to execute the java.exe file directlty in linux I get error
-bash: ./java.exe: cannot execute binary file
Any suggestions on how to resolve this issue?
You shouldn't refer to the jre/ subfolder, as Maven will need a Java compiler and potentially other JDK-only tools.
So just use:
JAVA_HOME=/home/z222189/jdk1.8.0_31
EDIT: (making sure that a correct, working JDK is installed at that location, of course...)
Seems you are in Linux, be sure you get Java for Linux!
Set JAVA_HOME using
export JAVA_HOME=/home/z222189/jdk1.8.0_31
Does not work because java is not there, or the version is not the correct, if you already installed correct java linux version and you dont really know where your oracle-java is installed, you can find this running
which java
which will tell you which binary of java is being called, normally is something like
/usr/bin/java
but you can
readlink -f /usr/bin/java
that will give you something like
/usr/lib/jvm/java-7-oracle/jre/bin/java
ergo
export JAVA_HOME=/usr/lib/jvm/java-7-oracle/jre/

Tell Grails Only which JDK to use

I'm on Windows.
Grails project needs JDK 1.6. As this is a truly compromised version, I don't want to set it as my user or system JAVA_HOME. I only want grails to use the ancient version. does setting it in the command line with "set" change the path permanently or only for the running instance?
Using set to set the JAVA_HOME from the command line will only set that environment variable within the scope of the command/temrinal/console and not system wide. Assuming you are using cmd.exe.

JDK 32bit and 64bit on JAVA_HOME simultaneously

I am using smartGIT in Windows 8.1 which uses 32Bit JDK (1.8), I needed to set JAVA_HOME to 32bit directory
but now Im using as well Android Studio which needs JDK 64bit (1.8 installed), and I need to set JAVA_HOME pointing to 64bit directory
is there a way to set them both on the same variable and depending if the software requires either JDK 32 or 64 can pick the appropriate framework???
I tried to set them simultaneously by separate them with a semi colon like this
JAVA_HOME
C:\Program Files\Java\jdk1.8.0_25\;C:\Program Files (x86)\Java\jdk1.8.0_25
but this just works with 32BIT version, 64BIT is still doesn't work
thanks for the support
This is not a direct answer to your question. But I think this will help you to solve the underline problem of running SmartGit on 64bit JVM.
Go to bin folder under SmartGit installation directory. There you can find the "smartgit64.exe" executable to launch SmartGit in 64bit mode.
I have it running smoothly in Win8.1 64bit with 64bit JDK.
There is no way to have a static JAVA_HOME that satisfies version or bitness specific subprocess that I know of.
You don't say what platform you are on, but generally environment variables exported such that subprocesses can use them are fixed, are used as a basis for further tweaks, or are completely redefined.
But, what you can do is try to tweak the environment that those processes are called from so they have their own custom environment. For example, you could have your own JAVA_ROOT set, andf then use that as a basis for creating JAVA_HOME along with a hard-coded or script/function fetched environment variable for a specific JRE to make the "real" JAVA_HOME for everything run from this subshell.
Alternatively, if you are on OS X, there is a utility that can be used to set and fetch system information for installed JREs. This can be used in scripts, etc. Other platforms have similar tools.
Also, look hard at your toolset. Make sure that JAVA_HOME is the only way to set the VM it uses -- often this is just one of the ways you can set this.
[EDIT]
I forgot about the -d32 trick mentioned in the comments. That would be nice solution if it works, and you can use the same versions of the VM for all invocations.
[EDIT]
Ok, since you are on Windows, you could try launching each Java app from its own environment. A .cmd or .bat file if that is the way you roll, or you could create a Windows shortcut that you tweak before invoking the specific app.
For example, if one has a Java main they want to run from the command line, it is easy to wrap the entire command line in a .bat file. e.g.:
set JAVA_HOME=C:\Some\Path\To\A\JRE
set APP_HOME=C:\Some\Path\To\A\Jar
set JAVA_OPTS="-Dfoo=bar"
%JAVA_HOME%\bin\java.exe %JAVA_OPTS% -jar %APP_HOME%\myApp.jar
Now, running this jarfile will use the specific Java executable you set, and also pass the new JAVA_HOME environment to the Java process that it starts. This overwrites any "default" JAVA_HOME you have set in your User or System environment.
Any other processes will not see this JAVA_HOME, and when the .bat file exits the new definition disappears.
Alternatively, if you always start your Java apps or Java tools by double-clicking them, you might be able to wrap those applications in a Windows shortcut -- even if you just prepend the executable with "set JAVA_HOME=..." I always forget how tweaky this is, though, so I'm hand-waving a bit!

What is the difference between the JAVA_HOME environment variable and the output of "java -version"

What is the JAVA_HOME environment variable actually? I'm in doubt. What is done by setting it ? And I have also another question that if we have two or more JDK s in the OS, we can set one version's path to set the $JAVA_HOME variable and another version as the running java in the machine which is shown by "java -version" command.As I think in ubuntu, java --version command gives the JRE configured (running java) version of java and JAVA_HOME variable defines which version to use for compiling java programs. Am I correct? What is actually the difference between the jdks referred by these two outputs-
1. echo $JAVA_HOME
2. java -version
Lets start with what the two commands do.
echo $JAVA_HOME
This outputs the value of the JAVA_HOME environment variable. By convention, this environment variable is set (typically in a shell "rc" file) to the base directory of a JRE or JDK installation. However, none of the Oracle or OpenJDK core java tools depend on this. You set JAVA_HOME for the benefit of 3rd-party tools, to tell them which of possibly many JRE or JDK installations on the system to use.
So running echo $JAVA_HOME will typically give the directory for a JRE or JDK. But it might be unset ... or point to missing directory.
java -version
This runs the java command and tells it to output its own version information. Unlike the previous, if your shell can find a (real) java command, the information it outputs is definitive.
But which JRE / JDK does will the java command come from?
Well that is determined by the shell, and how it (in general) finds commands. For all mainstream Unix / Linux / BSD shells, this is controlled by the PATH environment variable. The PATH variable defines a command search path; i.e. a list of directories where the shell looks for commands.
So if PATH is /usr/bin:/bin:/usr/local/bin:/home/joeuser/bin:/usr/java/jre-x.y.z/bin then the shell will look for java in each of those directories in turn. The first one java command that it finds will be the one that is executed. You can find out which one it is by running this command.
which java
But that is not the end of it, because if you are using an OpenJDK Java installation on a typical Linux system, the java command in /usr/bin is actually the first in a chain of symlinks that is managed by the alternatives command. The actual executable is at the other end of the chain.
(The alternatives system is a way to implement a switchable system-wide default for a command or set of commands. Read the manual entry for more details. But note that Oracle Java installers (or RPMs) for Linux don't "grok" the Linux alternatives system ... one of my bugbears!)
Finally, it is common to see something like this in a shell "rc" file:
export JAVA_HOME=/path/to/jre # (or jdk)
export PATH=/bin:$JAVA_HOME/bin:/usr/bin:...
There is nothing magical about this. The $JAVA_HOME is going to be expanded by the shell before it sets the environment variable. Tthe actual PATH value will be:
/bin:/path/to/jre/bin:/usr/bin:...
So in summary:
echo $JAVA_HOME tells you the JRE or JDK that a typical 3rd-party tool will try to use by default.
java -version tells you the JRE or JDK that the java command comes from.
They may be different.
JAVA_HOME is an environment variable that contains the directory where Java is installed (if a few versions of Java are installed - this will point to the default)
java -version is a command that shows which version of Java is installed (and again, the one that's used by default in case a few versions are installed)
Addition: Thanks to Luiggi's comment: if the folder where java is installed is not in the PATH (another environment variable) - running java -version or any other java command will fail!
Its not related to jdk and JRE. java -version it is used by the java which is in /usr/bin/java. In some application is used $JDK_HOME. Both the jdks are same. Or we can define different jdk.
echo $JAVA_HOME
will print the value of environment variable called JAVA_HOME. If it's not set, it will print an empty message. Usually, you set JAVA_HOME to the path of the latest JDK installed in your pc.
java -version
will print the version of the java executable (Java Virtual Machine) that is set in your path, if configured. Otherwise, it will throw an exception on the command line.

Categories