Can I specify what version of Java to run my program on - java

I have an old program that runs on JRE 1.6, when you upgrade beyond that the program will not log in.
I've looked into upgrading it but cant seem to make it compatible with later JRE versions.
Problem is customers keep upgrading the JRE on the machine it runs on, even when told not to.
Is it possible to install 2 JRE on a machine and specify which one to use when running my program?
I was thinking if the class path points to JRE 1.8 but i didn't use the classpath but the full address of the JRE 1.6, would that work ?
Edit:
java -Djava.library.path=lib/ -classpath .;lib/Serialio.jar;lib/log4j.jar;lib/client.jar -Djava.security.policy=java.policy 192.168.0.10 1098
this is my bat file, i've tried replacing -classpath with C:\jre1.5/bin/java.exe -jar but it thinks thats the name of my main class not the new class path, what am i doing wrong

Is it possible to install 2 JRE on a machine and specify which one to use when running my program?
YES, if you execute the jar, you can run it from an specific JRE
path_to_jre/bin/java.exe -jar YourApp.jar
Simply put the above line in a .bat file for your customers and double click will execute the program with defined java version, allowing your customers to upgrade java for other apps without problem.
java -Djava.library.path=lib/ -classpath .;lib/Serialio.jar;lib/log4j.jar;lib/client.jar -Djava.security.policy=java.policy 192.168.0.10 1098 is my bat file, i've tried replacing -classpath with path_to_jre/bin/java.exe -jar it thinks thats the name of my main class not the new class path, what am i doing wrong
your bat file must be something similar to this (path to java is my own one):
C:\Program Files\Java\jre6\bin\java
-Djava.library.path=lib/
-classpath .;lib/Serialio.jar;lib/log4j.jar;lib/client.jar
-Djava.security.policy=java.policy 192.168.0.10 1098

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 ...

'java' is not recognized as an internal or external command, operable program or batch file

I am using java 12.0.2, JAVA_HOME is set as C:\Program Files\Java\jdk-12.0.2 and PATH is having %JAVA_HOME%\bin
still when I try executing java command its giving me the error 'java' is not recognized as an internal or external command, operable program or batch file.
java and javac are part of JRE and JDK 12 doesn't have JRE now, so how to setup this.
please don't relate this issue with old questions, until java8 it's fine but its not working now in windows 10.
java and javac are part of JRE and JDK 12 doesn't have JRE now, so how
to setup this.
After JDK 10, there is no separate JRE shipped and you do not need a separate JRE anymore. The JDK is enough.
I am using java 12.0.2, JAVA_HOME is set as C:\Program
Files\Java\jdk-12.0.2 and PATH is having %JAVA_HOME%\bin
The space in the path may cause problems. I suggest you set C:\Program Files\Java\jdk-12.0.2\bin directly into PATH. Also, make sure to promote this entry to the top (beginning) so that this entry overrides the existing Java version (if any).
Last but not the least, start a new cmd window session to check java -version because the changes in the environment variables using the GUI does not become effective in already open cmd windows.
For any misconfiguration, if you can not make it work, download a fresh JDK and follow the instructions on the download page.
Try removing any existing java paths and making sure your JAVA_HOME is correct. There is no JRE but the java command should work if your env vars are correct. It is most likely a configuration error so deleting old configuration and reconfiguring will probably hlep.
The problem was caused by Python which was not allowing to execute java/javac commands. Once I uninstall the python everything worked fine.
I am still not sure what happened when I install Python as I explicitly didn't do anything except enabling the tick mark to setup the path variable while installing it.

Run a Java program with java.exe in the same directory without installing

I know there are lots of posts similar to this, but all of their answers require creating an .exe file. I want to know how to run a program in a folder without installing java.
I don't want to install Java 8 as it breaks some of my programs. I want to run a java program in a folder using a start.bat file that points to the java.exe file in the same directory. Like this:
"java.exe" -jar program.jar
The error is like this:
Error: Registry key 'Software\JavaSoft\Java Runtime Environment'\CurrentVersion'
has value '1.7', but '1.8' is required.
Error: could not find java.dll
Error: Could not find Java SE Runtime Environment.
Since the command window is opened in the same directory, "java.exe" should point to the java runtime.
Can I maybe copy java.dll to the directory to make it work? How can I do this without installing java 8? Could I use java ME?
Why don't you create a .bat file that declares a new JAVA_HOME and appends to the PATH just for this DOS/batch session, then you can point to a place where you have the required JRE (however, you kind of have to install it... it's a Windows thing).
I understand you are reluctant to install the JDK/JRE 8 on your Windows because of this: "I don't want to install Java 8 as it breaks some of my programs". But trust me, you can always change the JRE version on the Java Control Panel (windows >> control panel >> Java) and change the JAVA_HOME & PATH environment variables, you might have problems with your browser's plugins though, the Java install will affect them as well.. I guess, in that, case you can either try to skip this part of the install process or.. install JDK 7 again afterwards?
IF you just want to run this program, you can also install VirtualBox and create a VM with windows and install the JDK8 there, avoiding any problems in your OS.

How to run a specific version of Java with a program?

So I have the latest version of JRE 7 on my computer, and everything is working fine. Lately, I've downloaded a program that requires JRE 6. I know where all of the files are located on my computer, all I'm asking is the .bat file code to run a specific version of Java with only that program. I am somewhat of a newbie when it comes to Windows and Java PATH structure, so
Stanford's computer science course has you use a modified version of Eclipse to code in Java, and it was created when Java was still in version 6. If you are familiar with this, then you may know of karel the robot, a Java application that opens in Eclipse. When I tried to run this, the Karel program did not appear; only a blank screen. I found a youtube video about using Karel and many of the people in the comments had been having this problem, and some said that using JRE 6 fixed it. Also on the installation instructions, it said to use JRE 1.6, but I thought it would work with JRE 7
you can call each java.exe directly.
You can create 2 batch file named java6.bat and java7.bat :
java6.bat
#echo off
"C:\Program Files\Java\jre6\bin\java.exe" %*
java7.bat
#echo off
"C:\Program Files\Java\jre7\bin\java.exe" %*
to call a program with jre6
java6 -jar helloworld.jar
and to call a program with jre7
java7 -jar helloworld.jar
If you mean this program then I had no problem launching it with java 7
However if you really need java 6 for operating it then you could write some .bat file like this:
#echo off
set JAVA_HOME=C:\oracle\jdk1.6
set MY_CLASSPATH=lib\karelj.jar;lib\silk.jar
%JAVA_HOME%\bin\javaw.exe -cp %MY_CLASSPATH% karel.swingui.Main

"Could not find the main class. Program will exit"

I'm trying to run SQuirreL SQL.
I've downloaded it and installed it, but when I try to run it I get this error message:
Java Virtual Machine Launcher.
Could not find the main class.
Program will exit.
I get the gist of this, but I have not idea how to fix it. Any help?
more info:
I'm on Windows XP pro.
I have java 1.6 installed, and other apps are running OK.
The install ran OK.
I believe I've followed the installation instructions correctly.
To run it, I'm invoking the squirrel-sql.bat file.
Update
This question: "Could not find the main class: XX. Program will exit." gives some background on this error from the point of view of a java developer.
Is Java installed on your computer? Is the path to its bin directory set properly (in other words if you type 'java' from the command line do you get back a list of instructions or do you get something like "java is not recognized as a .....")?
You could try try running squirrel-sql.jar from the command line (from the squirrel sql directory), using:
java -jar squirrel-sql.jar
The classpath is the path that the system will follow when trying to find the classes that you're trying to run. In the batch file you're trying to execute it probably has a variable like CLASSPATH=blah;blah;etc or a java command that looks similar to
java -classpath "c:\directory\lib\squirrel-sql.jar" com.some.squirrel.package.file
If you can find or add that classpath setting, make sure that it includes a path to the squirrel-sql.jar and any other jar files that it may depend on separated by semicolons (or the root /lib directory that may be included with the installation).
Basically you just need to tell java where to find the class files that you're trying to execute. Wikipedia has a more indepth discussion about classpath and can offer you more insight. http://en.wikipedia.org/wiki/Classpath_(Java)
JAVA_HOME variable must be set, to point to the prog files/java/version???/bin
open squirrel-sql.bat file with some text editor and see if the JAVA_HOME variable there is the same as the one in your enviroment variable
change it if it doesn't match....and than run bat file again
Have you followed these instructions:
http://www.squirrelsql.org/#installation
If so, are you running the batch file or the shell script to run it?
Tweaking MB's answer for windows, will get rid of the console window:
start javaw -jar squirrel-sql.jar
The .bat file does not seem to work.
Just double-click on:
squirrel-sql.jar
or type:
java -jar squirrel-sql.jar
in the command-line.
You can place .; in classpath in environmental variables to overcome this problem.
I tried to start SQUirrel 3.1 but I received a message stating "Could not find the main class Files\Rational\ClearQuest\cqjni.jar" I noticed that C:\Program Files\Rational\ClearQuest\cqjni.jar is in my existing classpath as defined by the Windows environment variable, CLASSPATH.
SQUirrel doesn't need my existing classpath, so I updated the SQUirrel bat file, squirrel-sql.bat.
REM SET SQUIRREL_CP=%TMP_CP%;%CLASSPATH%
SET SQUIRREL_CP=%TMP_CP%
It no longer appends my existing classpath to its classpath and runs fine.
I had this problem when I "upgraded" to Windows 7, which is 64-bit. My go to Java JRE is a 64-bit JVM. I had a 32-bit JRE on my machine for my browser, so I set up a system variable:
JRE32=C:\Program Files\Java\jre7
When I run:
"%JRE32\bin\java" -version
I get:
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)
Which is a 32-bit JVM. It would say "Java HotSpot(TM) 64-Bit" otherwise.
I edited the "squirrel-sql.bat" file, REMarking out line 4 and adding line 5 as follows:
(4) rem set "IZPACK_JAVA=%JAVA_HOME%"
(5) set IZPACK_JAVA=%JRE32%
And now everything works, fine and dandy.
I had the same issue with a different application (BI Publisher) because I installed a 32 bit version of this application on a 64 bit version of Windows.
Java Virtual Machine Launcher - could not find the main class
The solution for my case was to tell BI Publisher where to find the x86 version of JRE:

Categories