I created a java text based game. I exported it as a runnable jar file in eclipse, but double clicking didn't work to run it (not sure why this is, would appreciate an explanation) so I created a .bat file which has:
java -jar game.jar
This works on some computers. However, when a user who doesn't have java in their PATH (I assume this is the reason) runs the .bat file, it comes up with the error:
java is not recognized as an internal or external command
How can I make my java game able to run on all computers that have java (1.6) installed?
java -jar <your-jar> is, I think, the best you can do, if you want to support different operating systems as well. If a user doesn't have java command in the path, java support has not been installed as a public JRE and user should either fix the configuration or explicitly specify which private JRE is to be used. Oracle documentation explicitly states about the public JRE that
You must set the PATH environment variable to point to JAVA_HOME\bin
(where JAVA_HOME is the location where you installed the public JRE)
to register the JRE.
so a user should do just that. You can, however, check if JAVA_HOME is set and if java exists in the path and provide informational error message if it is not.
If you're fine with only supporting windows, you can use various .exe packaging systems to ease the process for end user. See this, this and this thread for details.
Related
I am getting "A JNI error has occurred, please check your installation and try again" error Whenever I'm running my java jar using "java -jar filename.jar" command with windows power shell. It works fine whenever I opens it with double click.
How Can I resolve this issue?
Thanks in advance.
You say it works when you double click the JAR file (in File Explorer) but not when you run it from the PowerShell window in this folder:
C:\incubating-netbeans-11.0-bin\netbeans\ani\Tank-IQ-Display-Configurator\Tank-IQ-Display-Configurator\dist
I'm guessing you have more than one JDK installed.
If you enter the following command in the PowerShell window, it will display the paths to all the java.exe files.
where.exe java
Windows associates file extensions with executables. Obviously, on your computer, the .jar extension is associated with java.exe. You can check this via Control Panel. Look for Default Apps. Hence when you double click the JAR in File Explorer the associated executable is launched.
I'm guessing that the default executable is that of JDK 11 and that either in the folder whose path I wrote above there is a java.exe that is compatible with JDK 8 or in your PATH environment variable, the path to the JDK 8 executable comes before the path to JDK 11 executable.
So check those things, i.e.
Default apps in Windows
PATH environment variable
Obviously there are many different ways to rectify the situation. I don't think any one is clearly superior and the most appropriate would depend on your environment and your needs which I am unaware of since you haven't provided those details, hence I won't suggest what actions you should take in order to resolve your issue.
What you do need to do is ensure that a JAR file containing java code that was compiled to JDK 11 is run with a java.exe from at least JDK 11.
Note that higher java versions can run classes compiled to lower versions. In other words, if your JAR was compiled to JDK 8, you could run it with JDK 11, but not the other way around (which is the cause of your error, as others have indicated).
As it says quite clearly in the error message, your JRE is too old. Install a newer JRE (and update your PATH and JAVA_HOME).
Also, please do not post error messages as screenshots when you could also paste them as text.
I have a program which seems to only work with a later version of java (14.1.03), however I don't want to uninstall my current up-to-date java version. I was wondering if it's possible to have 2 instances of java in the path on windows 10 and be able to compile programs through the command prompt with choice on which instance of java has compiled the program.
Thanks in advance!
Basically, putting multiple Java installation directories onto %PATH% won't work.
The %PATH% variable is a list of directories for the command processor to search to resolve command names. When you tell the shell to run java, it looks at each directory in turn until it finds an executable command that matches java (e.g. java.exe, java.bat, etc). Then it stops looking.
So if you have Java 11 and Java 14 on the %PATH%, one of them will be earlier the list than the other. The Java commands will come from that Java install, and the other install will (typically) be ignored.
Solutions:
One approach is to set %PATH% according to the Java install that you want to use right now:
You could do this by embedding a local %PATH% setting in the batch file you use to launch your application.
You could do it interactively
You could do it by having user (or account) specific %PATH% settings.
Another approach is to use absolute command names; e.g. C:/JDK11/bin/java or C:/JDK14/bin/java instead of java. (Use appropriate paths of course.)
(On Linux, there is a nice tool called alternatives that can be used for managing and switching default commands. It does it using symlinks ...)
I am newbie to MAC platform.
I need to know if I really need to set JAVA_HOME path on MAC OS.
Previously I have used windows and from what I have learned is, user need to specify JAVA_HOME path in "Environmental Variables" so that the system will know if JAVA is really installed in computer.
But is such thing not mandatory to do in MAC?. Can I code right away after java installation?
Can someone explain
NO,
you don't have to set JAVA_HOME path to run java code in eclipse. Though to run the code you will have to set the Run configuration, and specify the JRE you want to be used, to run the code (under JRE tab of Run Configuration). Here you will browse and point your system to access the required (Installed) JRE.
Though if you set JAVA_HOME path, you would be able to call the compile and run for java code from command line (Terminal) as well. and Eclipse will also pick the same without setting it up explicitly.
You can code right away; the eclipse oomph installer will hardcode a link to the location of your java vm for you in eclipse's config, which means eclipse will start regardless of your PATH or JAVA_HOME variables.
When running java apps from within eclipse, eclipse has its own registration of where java vms live. You can install various versions of java; you can tell eclipse about any or all of them and pick the VM that goes with a given project (the default is in windows | preferences, search for 'jre' in the filter – and you can also override this default in the project; right click on a project and select properties).
Any attempt to run stuff within the project will use the project's configured VM, and if there isn't one, the default as configured in the window|preferences dialog.
Is it possible to bundle a JRE within an exported stand alone Java app? We have a very specific requirement to run a standalone AnyLogic Java app on a machine that does not have the latest Java version installed and due to company IT policies we will not be able to do so
Through some research I have found some sites to claim that they have already been doing it for Windows and Mac.
Using a bundled JRE on OSX
https://wiki.openjdk.java.net/display/MacOSXPort/How+to+embed+a+.jre+bundle+in+your+Mac+app
http://www.intransitione.com/blog/take-java-to-app-store/
My issue is that most of these posts refer to bundling an app for Mac OS x and requires that the jar files be created in an IDE like Eclipse. But since I use AnyLogic the jar files gets exported without myself being able to intervene. What I need is to change the command line code that runs the jar files and currently looks like this:
java -Xdock:name="AnyLogic Model" -Dnativewindow.awt.nohidpi=true -cp com.anylogic.engine.jar:com.anylogic.engine.nl.jar:lib/database/querydsl/querydsl-sql-codegen-3.6.3.jar -Xmx256m model6.Simulation $*
(Note: Code reduced for readability)
into something that I assume will pass the jre or JVM to be used as an argument to the java call. Or maybe set the directory to be used for java or something... as calling the java command on a machine without java installed renders nothing.
I have a very simple app, as well as a jdk plugin that I got from the moneydance app, which is a java app that runs on OSx with its own embedded jre, available here
https://www.dropbox.com/sh/1bedimsb0lj403t/AADYR7iFoBD4YiqS_RGZ2xAVa?dl=0
Thanks
A colleague of mine who is not on Stack Exchange gave me the answer so here goes, actually quite easy:
In order to meet my specific circumstances one just needs to include a jre inside the root of the folder that you supply to a client and then reference the the java executable in the execution file. The solution for Windows and Mac are slightly different so here goes:
On Mac
You can find the jre in the following folder. It is a hidden folder so if you Mac is not set to show hidded folders go to finder use command-shift-g and go to
/Library/Java/JavaVirtualMachines/
there should be a jdk folder and then navigate to
jdk1.8.0_45.jdk/Contents/Home/jre
On Windows
The location of the jre is in
c:\Program Files\Java\
you can see the location in the .bat file that AnyLogic creates automatically in line of code that looks like this:
#SET PATH_XJAL="%DISK_XJAL%\Program Files\Java\jre6\bin\java.exe"
Once you have the jre copy this folder to the same location as the stand alone java app. Then the only thing that remains is to change the referenced location in both mac command line executable and the windows.bat file
On Mac
Change from
java -Xdock:name="AnyLogic Model"
to
./jre/bin/java -Xdock:name="AnyLogic Model"
On Windows
Change from
#SET PATH_XJAL="%DISK_XJAL%\Program Files\Java\jre6\bin\java.exe"
to
#SET PATH_XJAL= \jre6\bin\java.exe"
Running the java app on both Mac and Windows will now be independent from the Java version on the machine or whether it is installed or not
another alternative is to compile your compiled application into an executable using Exe4J. It is not free but another advantage is that you send your client only a single exe-file, not a bunch of files. (Disclaimer: not tried with the internal AL7-dbase yet but it works fine when accessing external data).
I try to convert a jar file to an exe file.
I followed the instructions provided here using launch4j .
I get only an XML file but no exe as expected.
Any ideas?
So how should I deploy my application?
Your user would have to have the JRE installed on their computer. However, you could also distribute java.exe and rt.jar (the java run time jar) as well as any library jars you need for your application along with your program. Since the java command has an optional classpath parameter you could write a batch file (something like runme.bat) which would look something like this:
java -cp rt.jar;myapplication.jar;otherjars.jar MyMainClass
This is still Windows specific because the java.exe file would be a Windows specific file. You could also add in logic to check for the environment variable JAVA_HOME and if that's set on the user's computer then the java program specific to their OS would be in JAVA_HOME/bin
Your real problem comes when you're not sure what operating system your user will be using AND you don't know if they have Java installed. If you simply require that the user has the Java Runtime Environment installed then you're problem is solved for all users.
I hope that helps.
Greg