Check jdk/JRE is installed and get path for jvm.dll - java

Prgrammatically, how to check if java is insalled and get path from jvm.dll ?

Java can be installed anywhere, and there is no guarantee that you should be able to see, where. So, the general answer to your question, is "it is impossible", or that "you had to run a recursive file search for jvm.dll on your whole filesystem".
But this is not, what you want. I think, you want to get a "default" java or jvm, which will be used by java tools or any java software.
Normally you can find that in the $JAVA_HOME environment variable (on windows, %JAVA_HOME%) the actual jdk or jre install, which should contain jvm.dll.
If it doesn't work, it is a configuration problem on the system, but maybe it can happen. Your second choice should be to find a java.exe or a javaw.exe somewhere in your system path ($PATH or %PATH% on windows).
There are a lot of tools, or java softwares which are coming out with their own java/jvm instance, especially oracle like to give out them. In their case, only the general solution works.

You can also Write a Java program that prints out the value of the java.home system variable. For example:
System.out.println(System.getProperty("java.home"));
then invoke java.exe on this class. If Java is not installed or you can't run it then the process will not start... but if it starts then it'll tell you the path to the JRE installation folder, which is what you need in order to figure out where the jvm.dll is.
There's still the problem of Java being installed but not being on your PATH. In that case, I would just report that Java could not be found and ask the user for additional input.

This is a snippet of how I did it, the location of the jvm dll will be stored in runtimeLib.
char version[255];
char runtimeLib[255];
DWORD BufferSize = 8192;
string javaKey = "SOFTWARE\\JavaSoft\\Java Runtime Environment";
if(RegGetValue(HKEY_LOCAL_MACHINE, javaKey.c_str(), "CurrentVersion", RRF_RT_ANY, NULL, (PVOID)&version, &BufferSize) != ERROR_SUCCESS)
//jre not found
javaKey += "\\";
javaKey += version;
BufferSize = 8192;
if(RegGetValue(HKEY_LOCAL_MACHINE, javaKey.c_str(), "RuntimeLib", RRF_RT_ANY, NULL, (PVOID)&runtimeLib, &BufferSize) != ERROR_SUCCESS)
//jvm.dll not found

Location of VM Library Files (jvm.dll)
For Oracle's JDK. The path within the JDK to the Java HotSpot Client VM is:
jre/bin/client/jvm.dll (on x86)
The path to the Java HotSpot Server VM is:
jre/bin/server/jvm.dll (on x86)
jre/bin/server/jvm.dll (on IA64)
Windows Registry Keys
Oracle, OpenJDK, Azul, IBM and other JDK developers may use a variety of Windows Registry Keys. For example, Java 11 appears to use the JavaSoft\JDK key, while Java 8 uses JavaSoft\Java Development Kit. Programs like Launch4j and Java Service Launcher search the registry for JVMs.
HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Development Kit\{version}\JavaHome
HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Plug-in\{version}\JavaHome
HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Runtime Environment\{version}\JavaHome
HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\JDK\{version}\JavaHome
EDIT
# Recursively search JAVA_HOME for jvm.dll to set JVM_FILE
FOR /R %JAVA_HOME% %f IN (*) DO IF "%~nxf"=="jvm.dll" SET JVM_FILE="%~dpnxf"

Related

How important is setting environment variables in Java?

I am writing a blog where I have explain my students how to install Java, but I'm wondering why should expose them to this complicated part of Java installation. When I used Java before, I used right out of the box with my IDE. But all the tutorials I see mention configuration of these environment variables.
Maybe my PATH variables were set from previous installations, however I don't remember doing it.
Can anyone please advise if environment variable setting can be left to the IDEs or has to be done manually?
Java does not need any environment variables to be set.
However, setting some environment variables makes some things easier.
PATH If the jre/bin folder is on the path, you don't have to qualify to run the java command. If the jdk/bin folder is on the path, you don't have to qualify to run the java and javac commands. As well as some other commands provided by Java.
JAVA_HOME Used by many Java programs and installers to find Java.
When using an IDE, it can usually find / prompt for Java installations, so it doesn't need any environment variables. The IDE won't set them either.
As mentioned above, JAVA_HOME Used by many Java programs and installers to locate Java. for instance, Tomcat requires the environment variable JAVA_HOME to be set to the JDK installed directory.
To check if JAVA_HOME is already set, start a terminal and issue:
echo $JAVA_HOME
JAVA_HOME is to be set to the JDK installed directory. You need to find your JDK installed directory.
[TODO] find macOS and Ubuntu JDK installed directory.
Add the the following line at the end of "~/.bashrc" (or "~/.login"). Take note that filename beginning with dot (.) is hidden by default.
[TODO] How to un-hide for macOS/Ubuntu.
export JAVA_HOME=/path/to/JDK-installed-directory
// Verify the new setting
echo $JAVA_HOME
I installed Java and then installed Eclipse, and you know what, it worked fine, however for one of my colleagues it was giving a "warning" regarding not being able to find Java, and error was clearly showing it needs environment variables because it wasn't able to find the path to Java, like Java wasn't installed, while it was installed there.

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!

Switch between 32-bit and 64-bit JDK platform on Windows 7

I am using Windows 7 on my laptop. I am working with jpcap libraries which work only on 32-bit JDK. Having both 32 and 64 bit JDKs installed on my machine. How can I switch between 64-bit and 32-bit JDK?
I tried changing the %JAVAHOME% and %PATH% environment variables, but it didn't work. Should I change anything in the registry?
#Srikant Sahay, Thank you very much! It did work on widnows 8.1 64 bit OS.
I am able to Switch between 32-bit and 64-bit JDK platform on Windows 8.1 on demand. The trick, as Srikant suggested, is to set the path before all other path variables. Make sure you put fist in the path variable.
java -d32 -version ( or simply )
java -version
java version "1.7.0_51"
Java(TM) SE Runtime Environment (build 1.7.0_51-b13)
Java HotSpot(TM) Client VM (build 24.51-b03, mixed mode, sharing)
However, only one instance works at a time. Either you choose the 32 bit version or the 64 bit version. So, if you type "java -d64 -version" you will get
Error: This Java instance does not support a 64-bit JVM.
Please install the desired version.
Therefore, if you instead want a 64bit JVM you should put the JDk installation folder(in my case "C:\Program Files\Java\jdk1.7.0_51\bin" since the OS is 64 bit by default) first in the path variable.
cheers!
You can either set your path, or switch between JDK's in your IDE. Personally I use JetBrains IntelliJ IDEA and set the JDK via the IDE.
Setting via IDE:
From within a project:
click File -> Project Structure
Select Project under Project Settings
Select the Project SDK. If yours is not listed, click New -> JDK and browse for the JDK
From a new project:
Click File -> New Project
Select the Project SDK. If yours is not listed, click New -> JDK and browse for the JDK
If you want to set your path:
Follow the directions listed on the java.com site.
Keep in mind that your jar will run using whatever version of java is specified in your PATH.
If your PATH variable is not working, try cleaning up your path variable by removing unnecessary entries and rebooting. After reboot add Java to the path and reboot again.
If your still having issues, try executing using the absolute path of the java version your trying to use to ensure it works. If it does work, check your path variable against it to ensure it is accurate.
No, don't touch the registry. Each running Java application gets a single unique JVM instance. So a single java program cannot run some parts of the code in one JVM and other parts of the code in another JVM, as far as I know. You can, however, choose which JRE a particular java program runs in, so you can run the entire jpcap program in your 32 bit java version. Use a startup script (batch file) that points to the exact location of java.exe. When you run "java MyClassName" it's really just finding the java executable on the PATH environment variable, so by explicitly specifying your path, you get to choose your version. In other words, if you run a java program using "java MyClassName" (or if a program on your machine does so) it will use the java.exe application found on your PATH environment variable. Applications themselves can use whatever environment variables they choose: typically the ones used for java are JAVA_HOME and JRE_HOME. So you might want to try setting both of those and then trying to run your program again.
Set the path of your java in System Environment Variables PATH variable. Set it to be before any other path (even System 32 if java or javaw is present there).

How to check is Java is installed on Windows?

How to check, is Java is installed on Windows systems ?
You can read this out of the registry. Look in this key:
HKEY_LOCAL_MACHINE\Software\JavaSoft\Java Runtime Environment
and read the string value named:
CurrentVersion
This setting lives in the 32 bit view of the registry, so if you ever build 64 bit code make sure you include the KEY_WOW64_32KEY flag.
Visit the officail website http://www.java.com/en/download/testjava.jsp and click "test currently installed version of java"
Some information which might be helpful:
a Java run time environment (JRE) can be installed by simply copying it to the computer (no Windows system entries are required)
there can be many different JRE versions on the computer at the same time (many commercial apps are bundled with their own JRE)
your Delphi app could check whether there is a java.exe in the system path (and so avoid scanning all folders)
the JAVA_HOME environment variable normally indicates the location of the Java Development Kit (JDK), but in a typical installation the JDK directory also contains a JRE folder
For my Inno Setup I check this key :
HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Applications\javaw.exe
If I remove JAVA the key doesn't exist anymore.

Finding the JRE on Windows

I know this question is going to sound very stupid but here goes nonetheless. I need to bundle the new version of the JRE with my applicaiton and I cannot find either a version of the JRE that is not in .exe nor can I find where the jre is installed to on Windows 7 (windows 7 search cannot find anything so it is not helpful). Can anyone tell me where I can download a version of the JRE the would be good to bundle or where I can find the path that windows installed the JRE too?
Not sure about Windows 7 but on Windows XP the installation defaults to C:\Program Files\Java\jre6
corsiKa is correct about Windows 7
I found that the file path for jre is
C:\Program Files (x86)\Java\jre7
For my purposes I needed to install the Connector/J JDBC driver in the ext directory.
jre7\lib\ext\
Alternatively, install a JDK, if you haven't done it yet, and take the jre folder in its installation directory.
The JDK can also install it in Program Files (64-bit on 64-bit Windows, always on 32-bit Windows) or Program Files (x86) (32-bit on 64-bit Windows) as explained above.
It also installs java.exe, javaw.exe and javaws.exe in C:\Windows\system32
It will be the last installed version...
I have found another, more generic solution that I'm using in Powershell. The problem is that Java is now using symlinks to java, javaw and javac, so you can't always rely on using "where.exe java" because it returns the symlink.
I now rely on Java to report where it's actually running from by using verbose mode and parsing the output.
$javapath=((java -verbose -version | ? {$_ -match "Opened" }).replace("[Opened ","")).replace("\lib\rt.jar]","")
It will find the path that java reports it's actually using and return the installation directory. The only problem I haven't quite resolved is that it outputs extra information because of the "-version" option, but the only other option is the help, which is worse. However, when run from a script, the console output can simply be ignored. If someone else has a way of keeping it quiet, I'd like to hear it.

Categories