check java is installed on windows system - java

How to check, java is installed on windows system and get fullpath from jvm.dll on client folder?
I used this code, but i dont know how to retrive the current version
TRegistry* reg = new TRegistry(KEY_READ);
reg->RootKey = HKEY_CURRENT_USER;
reg->Access = KEY_ALL_ACCESS;
bool openResult = reg->OpenKey("SOFTWARE\\JavaSoft\\Java Runtime Environment", true);
if(!openResult) // cannot create registry keys, use default values
return;
if(!reg->ValueExists("CurrentVersion")) //it's not exist CuurentVersion
return;

I don't think writing some code or anything like that is needed to see if java is installed.
To see if its installed on windows just go to C:\Program Files\Java if that directory exists then you have java, if its installed on a different directory look in windows programs and it should show if java is installed, if not then you most likely don't have java.
Use this link to see what version you have, but it is recommended to always get the latest version.
Hope this helps, Luke
Edit:
If you want to see if java is installed on another computer programmatically try using a .bat file to see, this link should help.
or use jsmooth to build the jar into an .exe, with this, if java is not installed it will prompt the user to install java.

You can execute a system "Java -version" command from your code. Here is an example for C++. To find the version you just need to parse the result you get, if you get. The String result would provide the information you required.
Meanwhile, to find the path i believe you can do it by iterating through environmental variables and look for JAVA_HOME.

Related

Get JRE version from application

I need to get the java version from my application that the user has currently installed on their machine. However, the application that I have is installed with a self-contained JRE and when I do a System.getProperty("java.version"), it only returns the version of that self-contained JRE. Is there anyway that I can get the version that is installed on the machine?
The JRE installation are listed in the registry in the key only for Windows, Linux do not have central registry.
HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Runtime Environment
You can make a simple program to test :
public class ShowVersion {
public static void main(String args[]) {
System.out.println(System.getProperty("java.version"));
}
}
Or you can try command prompt
Type java -version
For more you can refer sister site of SO :
https://superuser.com/questions/1221096/how-do-i-check-what-version-of-java-i-have-installed
if you're bundling a JRE (did you check the distribution license for it? Make sure you are allowed to do so) and it's running under that, you get that version back.
If the user were to run it under another JRE, you'd get the version of that JRE. That's just how things work.
In fact if you're using a self-contained JRE the user doesn't even have to have another JRE installed on his system at all, that's the entire point of bundling one in the first place.
Apart from a full file system scan it's impossible to know what other JVMs might be installed, and if you do that you'd have to account for all the different names the Java runtime executables may have depending on the files system you're running on. And after finding all those executables you still have no real way of knowing what version of Java they belong to unless you either do a binary analysis of the executables or somehow detect the information from other files in the directories where those executables are installed, files that may or may not be present depending on the system in question, how it was set up, what JVM is in use, and whether the installation has been manually altered or not.
For example, in the root directory of a JDK installation there is a file called "release" which contains the JVM version, but AFAIK that file isn't required to be there for the JVM to work properly. And your application may not have the rights to the file system to read that file.
You can try looking in known paths e.g. C:\Program Files\Java but unless you scan the entire file system for java or java.exe you will never be sure.
This might not work as expected as in the user that runs your application shouldn't have access to system directories. You can force you application to be started with administrator level access but that would be a security risk.
You requirement seems a bit pointless if you are already bundling a JRE with your application.
You can run cmd command using java (java -version) and use that output to get version.
List<String> cmdArgs = new ArrayList<>();
cmdArgs.add("cmd.exe");
cmdArgs.add("/C");
cmdArgs.add("java -version");
ProcessBuilder processBuilderObj = new ProcessBuilder();
processBuilderObj.command(cmdArgs);
Process process = processBuilderObj.start();
//get inputStream
BufferedReader data = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line1;
while ((line = data.readLine()) != null){
System.out.println(line);
}
While System.getProperty("java.version") returns the version of the Java that your app is running on, calling "java -version" as a system command from your Java app will return the version of the default(installed) Java in your system.
Here are the generic steps to find it programmatically:
Using ProcessBuilder in Java, execute the system command "java -version". This is certainly platform-independent.
Read the output from executed process. The version is usually on the 1st line of the output (smth like java version "1.8.0_191"), so reading the 1st line is enough.
Check using regular expression, if the output matches a string containing a java version, you just parse the version and that will be what you are looking for.
Otherwise, it means the output is an error message (smth like -bash: java: command not found), and there is no Java installed in your system.
control panel
java
Java tab
view
you can see exact version of java used and it's location.

working with the command prompt in Java

I've been learning java on my own and I've come to a point where I need to pass arguments from the command prompt. I had previously been using Netbeans, which has become a bit of a crutch, but I want to learn how to program using notepad and the command prompt. I am running Windows 8.1. I downloaded the JDK 7 to my C:\ directory. I tried typing "java -version" and "javac -version" to check it out (as my book says to do) but it tells me "'java' is not recognized as an internal or external command, operable program or batch file." It says the same when I try javac. I do this from C:\ and from C:\Java> and get the same result. I uninstalled Netbeans, thinking maybe it was interfering somehow ... still not working. The book I'm using says it covers Java 7.
I don't understand why it doesn't work. Is there some compatibility issue with Java 7 and win 8.1? My java 7 is update 71 or 72. Did this update change something and now arguments are passed differently? Can anyone help me out? Thank you.
You need to download the JDK (if you don't already have it) and add the location to the end of your PATH. In a single command line session you can do this with PATH=%PATH%;<jdk location> where <jdk location> is the bin folder of the jdk.
Some installs of java manage environment variables and some do not. It appears yours has not. or you have not installed it correctly.
many applications understand JAVA_HOME, so you should set that to the root of the installation in your environment variables.
You will also need to add the bin folder to your path.
Generally, when using windows, you can install a windows exe version, which will manage all of this for you. Other installations just copy themselves to the c drive and expect java applications to know where to look.
Go to System environment variable. Select Variable PATH. Click on edit button. Append a ; after current value of Variable value. Copy jdk's bin folder path. In my PC it is "C:\Program Files\Java\jdk1.8.0_20\bin" without quotes. Then paste jdk's bin path. Copy jre's bin folder path. In my PC it is "C:\Program Files\Java\jdk1.8.0_20\jre\bin". Append a ; then paste jre's bin folder path. Click OK.
Download the JDK from this page. Run the installer.
Open the Command Prompt. Try it. It works. Yay!
If you use the full path to your java.exe, you dont need to set up any PATH and JAVA_HOME:
c:\> cd \work
c:\work> c:\java\bin\java.exe -cp classes\ your.Main
If your book does not use the normal JDK installers it really is supposed to explain that. (However it is normal behaviour for any executable).
(The above example asumes you compile into c:\work\classes\your\Main.class and your JDK is installed (installer does the unpack) in C:\java (typically you would use default locations like C:\Program Files\Java\jdk1.8.0\bin\java.exe).

'javac' is not recognized as an internal or external command

I have to convert my Matlab algorithm in Java. For this I'm using matlab builder ja toolbox, after following all the necessary steps, the build fails and this error is displayed:
'javac' is not recognized as an internal or external command, operable program or batch file. Error: An error occurred while shelling out to javac (error code = 1). Unable to build executable.
When typing java -version in the matlab command line, this is what i get:
Java 1.6.0_12-b04 with Sun Microsystems Inc. Java HotSpot(TM) Client VM mixed mode
It means I should have this compiler of java installed on my computer, only then the matlab builder ja works. I'm new to Java, I'm not sure what compiler I should be looking for.
First make sure you have installed jdk and jre, both are installed with the java software development kit. The installation folder is typically C:\Program Files\Java.
Now go to Computer > Properties > Advanced system settings in the advanced tab click on Environment Variables
In System variables find the variable named Path, in the value of Path you will find a java path like C:\ProgramData\Oracle\Java\javapath, just change it to C:\Program Files\Java\jdk1.x.x_x\bin.
Hope that helps.
If you want javac (the compiler), you'll need to download the Java Development Kit (JDK), not just the Java Runtime Environment (JRE). Note that the JDK includes a JRE.
This tutorial give instructions to install JDK to your computer.
You need to make sure that Java SDK is installed and also PATH is set properly so that windows would be able to know where the executable is. Detailed steps on how to debug this issue is explained here: http://www.windows-commandline.com/javac-not-recognized-internal-external-command/
To solve the aforementioned problem follow the mentioned steps:
1) Copy the address location of your Java\jdk\bin folder usually installed in your C: drive. It should look something like this:
C:\Program Files (x86)\Java\jdk1.8.0_65\bin
2) Right click on My Computer-->Properties-->Change settings-->
Advanced-->Environment Variables.. -->New..
3) Now,
Set Variable name: PATH
Variable value: C:\Program Files (x86)\Java\jdk1.8.0_65\bin
4) Press OK, re-open your cmd and compile your program.
Hopefully it worked!
Path is defined as the filename. Ex, C://aklsej;dlfkj/blahblah
You will need to specify the full path in the system control panel, advanced system settings, edit variables, edit path, put a semicolon after the last entry and don't delete anything, and then add the full path wherever you installed java to.
to make this easy, do a search for javac in the search panel and then just add the full path to the environmental variables from there.
Set Following Environment Variables
It will works
JAVA_HOME=C:\Program Files\Java\jdk1.7.0_09
set PATH=%JAVA_HOME%\bin
Download Java from given link according to your requirements.
http://www.oracle.com/technetwork/java/javase/downloads/index.html
then follow below steps:
Step-1 : Right Click on MyComputer and click on properties .
Step 2 : Click on Advanced tab
Step 3 : Click on Environment Variables
Step 4 : Create a new class path for JAVA_HOME
Step 5 : Enter the Variable name as JAVA_HOME and the value to your jdk bin path ie c:\Programfiles\Java\jdk-1.6\bin and
NOTE Make sure u start with .; in the Value so that it doesn't corrupt the other environment variables which is already set.
Step 6 : Follow the Above step and edit the Path in System Variables add the following ;c:\Programfiles\Java\jdk-1.6\bin in the value column.
Step 7 :Your are done setting up your environment variables for your Java , In order to test it go to command prompt and type
java
who will get a list of help doc
In order make sure whether compiler is setup Type in cmd
javac
who will get a list related to javac
Hope this Helps !
If you receive this error, Windows cannot find the compiler (javac).
Here's one way to tell Windows where to find javac. Suppose you installed the JDK in C:\jdk1.8.0. At the prompt you would type the following command and press Enter:
C:\jdk1.8.0\bin\javac HelloWorld.java
If you choose this option, you'll have to precede your javac and java commands with C:\jdk1.8.0\bin\ each time you compile or run a program. To avoid this extra typing, consult the lecture "Creating a Hello World application" in this website
Class names, 'HelloWorld', are only accepted if annotation processing is explicitly requested
If you receive this error, you forgot to include the .java suffix when compiling the program. Remember, the command is javac HelloWorld.java not javac HelloWorld.

Check if a program is installed on a linux machine from a java applet

Hi I need to be able to check if a certain software is installed on the clients computer and where, in order to launch it. I found the following three posts as to how to do so on Windows and Mac but I can't seem to figure it out for Linux as there is no registry. Does any one know how this can be done on Linux?
Similar posts for Windows and Mac:
Can a Java applet open a "select directory" and write to a filesystem via JavaScript interaction?
read/write to Windows Registry using Java
How can I see the software installed in a Mac OS using a java application?
any help would be greatly appreciated :)
Assuming your security context allows it, you could call out to which.
$ which java
/usr/bin/java
which will output nothing if the program is not found.
Use the
which file
command to find out if the software is installed in the path. If that comes up with nothing then you could do a
find ./ -name "file"
Also check their local bin or .bin if its not included in the path.
Well, basically every binary installed on Linux is in the PATH (environment variable), so if you can find it there, it's there.
There may also be software that installs into other paths, but in this case the user would need to point them out. It is a very uncommon case to have an application in a seperate path and not adding that one to PATH.

An easy bulletproof technique to check if the system has jre (windows)

Sorry my newbie question :P If I promp "java -version" in the cmd on a windows system, am I guaranteed that the system will be able to run .jar files if I don't get any error?
From the command line you should be able to invoke "java --version" which will return an error if java is not installed or the currently installed version information.
I guess the only guaranteed way to check for a JRE is to try to run a small Java program.
Or maybe not even that - I suppose conceivably a system could have only part of the Java standard library installed, in which case a small test JAR might work fine but a full program might not. Although I can't imagine why anyone would go to the trouble of setting a system up that way.
Why not run a small class file, which write a value to a file which you then check? If it fails, it doesn't work.
A good value might be the value of the java.version system property.
On Windows, you can check the registry at HKLM\SOFTWARE\JavaSoft\Java Plug-in. From there, each subkey is an installed JRE.
edit Here is C# code that will return an array of strings with the installed JRE's
public string[] GetInstalledJavas() {
// hold the registry subkeys that list the installed JRE's
string[] jres = null;
try {
RegistryKey myKey = Registry.LocalMachine;
myKey = myKey.OpenSubKey(#"SOFTWARE\JavaSoft\Java Plug-in"); // read-only
jres = myKey.GetSubKeyNames();
} catch (Exception myException) {
Console.Writeline(myException.ToString());
}
return jres;
}
Well, obviously not. You can put an empty file called java.bat anywhare in PATH, like C:\Windows\System32. Invoking "java" will not yield any errors but it doesn't mean there's a JRE installed.
I'd actually suggest, if you're only concerned about checking on windows machines, checking the registry for a handler for JNLP... that should guarantee the presence of a relatively recent JRE.

Categories