I'm currently a student at a high school, and I found a game programming competition for a club called FBLA. I had a few questions about the competition, and so I emailed them.
I made a post earlier on Computer Science Stack Exchange, where you can view here.
Now after receiving feedback from the question, someone commented that I should post my next question on this website.
My question is that how would I be able to package a Java JAR file with the native libraries, and then create an EXE out of it, so it will be a stand alone?
In my theory, I would have to create the jar, and put the required native files when a person installs java on their machine, into that JAR file, and then I can use a program such as FatJar or JSmooth.
If I'm wrong, or how I could understand a bit more on how jars are ran, it would help considerably.
Update Nov. 8, 2014
I finally got back around to messing with it, and I downloaded Launch4j. I set up and did everything with it, and I make an executable, which contains all of java's core native files. I uninstalled all of my programs dealing with java, and when I went to run it, it still required java.
Once again to state what I need to happen:
What needs to happen is the executable will be able to run even if there's not any version of Java installed on the judge's computer.
I would reccomend you use launch4j. http://launch4j.sourceforge.net
I've used it for some larger projects and it worked very well.
It can encapsulate your jar, dependencies and even a JRE into a windows in executable. It has a GUI or you can run from the command line.
Checkout Packr, created by libGDX team: https://github.com/libgdx/packr
To make your jar executable, you specify the main class of your application, any extra jar files it may require and so on in the jar's manifest file:
Main-Class: MyAppMain
Class-Path: MyLib.jar
Then you use the jar utility from the Java SDK to package your classes and resource files, specifying the m option and the name of your manifest file:
jar cvfm MyApp.jar MyApp.mf *.class *.gif
This will result in the creation of MyApp.jar. Now, if you type
java -jar MyApp.jar
the Java launcher will read the manifest from MyApp.jar, place that file and MyLib.jar on classpath and invoke the main method from the class MyAppMain.
Now, if your Java app needs native libraries, you also need to specify the java.library.path property pointing to the directory(ies) holding those:
java -Djava.library.path=.\bin -jar MyApp.jar
For more info and alternative solutions, refer to my article.
Related
I'm very new to java. I'm developing a tool that checks if your PC meets some set of specifications. This included writing and executing a separate batch file and including an API (SIGAR) for the model of the CPU.
My problem is that when I tried exporting it to Runnable JAR in eclipse, and I ran the resulting JAR from command line, it gave me lots of 'DLL not included in build path' Exceptions. After including the folder that contains the API DLL in the build path, I got similar exceptions. The thing that fixed it was adding the folder containing the DLL to environment variables (PATH) in Advanced System Settings.
Questions:
The JAR now works fine on my computer, but what about the users who download the JAR? Will they also need to add the DLL to environment variables? If so is there a way the JAR can do that for them?
My JAR won't run with a double-click, but will run from command line. Is there any way around this that will carry over to users who download the JAR too?
If the user downloads the tool and can't run it because they don't have the right version of the JRE, will the tool notify them? If not, is there a way around the user having to update JRE or will wrapping as an EXE suffice?
Thanks in advance, much appreciated. Lots of questions.
Q1: The JAR now works fine on my computer, but what about the users
who download the JAR? Will they also need to add the DLL to
environment variables? If so is there a way the JAR can do that for
them?
You can put a DLL inside a JAR file:
How to make a JAR file that includes DLL files? (Hint: read both answers ... completely.)
However, when you distribute a JAR containing a DLL, you then have the problem that different platforms require different DLLs (or whatever). Even with Windows you have the problem of 32 bit versus 64 bit DLLs.
Q2: My JAR won't run with a double-click, but will run from command
line. Is there any way around this that will carry over to users who
download the JAR too?
You cannot address that problem in a JAR file. The "double-click to run" functionality is implemented by the OS. The best way to provide this kind of functionality is using (platform specific) wrapper scripts that are double-clickable.
Q3: If the user downloads the tool and can't run it because they don't
have the right version of the JRE, will the tool notify them? If not,
is there a way around the user having to update JRE or will wrapping
as an EXE suffice?
Unless you have a JRE installed, the JAR file is just a passive blob of data. (A ZIP file, actually).
If the user has a JRE that is too old, then either the JRE will be unable to load any classes in the JAR (because the classfile version number is wrong), or you will get errors because of missing (system) classes.
The only hope would to do something like providing a wrapper script to launch your application that checked the JRE version before attempting to launch the JAR.
As a general rule, if you want to do fancy stuff like this you need to distribute your program in an installer, not as a bare JAR file.
I've developped a game during a Game Jam and I'd like to create an executable to distribute it to the other team members.
The game uses the slick2d and lwjgl library. I've tried to use JExePack, but the .exe file I get isn't runnable, I get an error while launching it.
Even the jar file gets me errors.
I'm only able to launch the game on the IDE. When I launch it with the command line : java -jar "game.jar", it obviously tells me that there's missing libraries, even if I indicate the path to the lib folder.
Is there an easy way to create an executable ?
Thanks in advance.
I think using a jar was a good idea.
You need to add every required jar in the classpath one-by-one for the jar to run properly.
Launch4J (http://launch4j.sourceforge.net/) is my favorite tool for that. You can just export an executable jar from your IDE and create an exe out of it. Creating an executable jar in Eclipse gives you the option to include all required libraries in it, which saves you from adding them manually when create the exe file. The minimal settings you need are:
Input (your executable jar)
Output (the .exe you want to create)
Minimum Java version (i.e. 1.6.0)
Thats it (as far as I remember)
If you want the exe you can use exe4j, it's a very useful tool, but i think using jars is better since you can run them on every platform. Anyway, when you export the jar, check on your ide's preferences if it automatically imports the libs. (for example, on eclipse you can pack the required libs into the exported jar)
You can use Luncher4j to create an exe file and convert the jar libraries to dll files.
I have exe file called Myapp.exe. Now I want to convert .exe to jar file. That jar file should also work in NON JAVA system. I don't have any idea to implement it. Can anyone please suggest me how to do it?
Direct conversion not available !!!
because they are in entirely different platforms.
Meeting your requirements is impossible for two reasons.
1) You cannot change an EXE to a JAR file.
2) You cannot run a JAR file on a system that doesn't have Java installed.
If you want to run something on a (Windows) system with no Java installation, it needs to be an EXE ... or something else that doesn't require Java.
(It might help if you explained why you think you need to do this. Perhaps there is an alternative set of requirements that are not impossible to meet.)
why i am doing all those stubs is for making my jar has to work in java not installed system.
It needs to be an EXE then!
I have an jar.It is working fine in java installed system.My task is to Bundle jre inside jar(Not along with jar(i.e we can put jre and jar in same folder to run a jar as given in following url mindfiresolutions.com/… ))Because i have to give jar file only to client,in such a way that they can use this Myapp.jar in non java system also.But,i don't know how to bundle jre inside jar.I Don't how to run jre inside jar?
Ermm ...
Is it possible?
No. You cannot embed a JRE inside a JAR file in any way that would allow it (the JAR file) to run your Java code without first installing Java. (And installing Java would defeat the purpose of embedding the JRE ... of course.)
But what you can do is create an EXE file which has a JRE and a JAR embedded in it. And there are tools for doing this. Here's the canonical Question on how to do it:
How can I convert my Java program to an .exe file?
I think you need to read the Oracle documentation on what a JAR file really is, and how Java programs are normally executed. That will help you understand what is feasible ... and what is nonsensical.
Jar available at http://dl.dropbox.com/u/10919879/roller.jar
After writing and testing my program with Eclipse's built-in run Utilities, I decided it was time to export and distribute. So I go to File>Export>Runnable JAR and designate the class whose main method I want to invoke. Everything seems to be going off without a hitch, until I try to actually run the JAR.
Error: Could not find or load main class C:\Users\Matt\Documents\My Dropbox\Publ
ic\roller.jar
Contents of roller.jar
dice/Roller$RollData.class
dice/Roller.class
META-INF/MANIFEST.MF
contents of META-INF\MANIFEST.MF:
Manifest-Version: 1.0
Class-Path: .
Main-Class: dice.Roller
What's going on, and how do I fix it?
It's not a problem with your jar. It's a problem with OS setting that launches jar files incorrectly. I don't know if currently on Windows after installing Java it will be correctly configured automatically and why it isn't on your system. It may require editing registry to set jar files to be launched with java -jar command.
You can use application that converts JARs to EXEs if you're going to distribute it only to users with Windows.
I am using JNI to wrap a few native functions in a closed-source PDF library. It has an dependent fonts directory which must be in a subfolder of the calling application's directory. In my experience, it is standard to seek based on the current working directory. Thus, the problem.
When loading the JNI code into a Java application, the current working directory is correct. However, the calling application's directory is java.exe's bin directory. I have verified that putting the dependent fonts folder in C:\Program Files (x86)\Java\jre6\bin folder works as expected.
The library seems to be using a C++ GetCommandLine() call, or something similar to determine where the fonts directory should be. Obviously, this is an unacceptable solution.
I'd like to avoid calling an external EXE. But the only workarounds that I've come up with are:
Compile an EXE, place in Java project directory, and use Java's Runtime.exec() to execute. (this does work)
Make JNI code launch a separate process which does the same as above (gains nothing but more complexity)
Any ideas on how I can circumvent this problem? When Java applications are compiled as a runnable JAR, is the resultant command line still the JRE's C:\Program Files\...java.exe?
A Java executable maker can create an executable *.exe from your Java application without any native coding or compiling. You can put that executable, the jar files, the fonts and other application dependencies into a single install directory.
Exe4j is one of the executable makers that will support this, for Windows. It does not require any assumptions about the current working directory. This is important in the frequent case where you have no control over what the working directory is when the application is launched.