I am working on a Java desktop application and want to execute an .exe file upon some user action.
I want to package the .exe inside the executable jar so that I have to ship just one item to my customer/user. But so far I have not been able to execute an .exe file which is packaged inside the jar.
One more input, the .exe in turn uses a bunch of .dll, .xml and .exe files (total 12 files), so I need to package all of them to make this work. Is this possible in java or should I use another language to achieve this?
I chose java because I want to support Mac platform also. So with these .exe and .dll files (for Win), I need to package and execute a bunch of Unix executable files and dylib files also (for Mac).
What would be the best way to achieve this?
I can think of 2 ways:
Make one Java executable app and dynamically execute exe or Unix executable file depending on platform it is running. The problem here is executing the files which are packaged within the jar.
Make separate applications for Win and Mac. In this case, which languages should I use?
Thanks much for the help!
Applications are packaged very differently on windows and mac. For the Windows version I would recommend you to distribute an installation program (see IzPack for instance) that will unpack the jar and the exe in the application directory (I don't believe you can directly execute an exe that's in a jar file). A MacOS application is actually a directory with a certain structure, so in your case, the said directory could contain both the jar and the exe.
Related
I have finished writing a project in the IntelliJ IDEA using Java. I can not figure out how to get the project from a saved file in the IDEA to an executable on my computer (I am on Windows 10 if that matters). Then, I want to be able to put the executable on my Girlfriend's computer (mac) because it's a gift for our anniversary. She's not very tech-savvy (apparently neither am I...) so I want to be able to just drag and drop something onto her desktop and have her be able to double click it and have it work without having to install anything to her computer. (I was looking at Jar files, but something I read said that mac's do not run Jar files unless I install some things).
So what I'm really asking is:
1)Using IntelliJ, how do I finalize my project (make a JAR file)? I have been unable to find directions for this that have actually worked.
2)What file do I need to make a batch file call to get it to run my program once it is a JAR? Does it just call the JAR as a whole, or a specific .class? (Someone suggested a bash file, but I have not heard of this before, so I am open to other suggestions besides batch files. I am currently looking into what bash files are)
3)Can I make the batch file call the file indicated in question 2 via relative path, thus allowing me to keep it in the JAR or in a folder with the JAR and making a shortcut to it on a desktop (for example)?
Building a JAR in IntelliJ
Option 1 - Long way round, getting IntelliJ to build your JAR
Hit Ctrl+Shift+Alt+S to open your Project Structure settings menu.
Go to Artifacts
Click the green +
Select JAR > Empty.
Name your JAR, for example Gift
Right click on all the files in the Available Elements that you want to put into your JAR (often everything) and select Put into Output Root
Click Apply. Click OK.
Go to Build > Build Artifacts... > Build.
You'll find your JAR in out/artifacts/{JAR_NAME}/{JAR_NAME}.jar
Option 2 - Short way (Maven only)
If you're using a dependency manager like Maven and you've adhered to the Maven project structure, just use the command:
> mvn clean package
and you'll find your JAR in your target/ directory.
In either option, you may have to define your .class file that contains your main() method.
Running a JAR
The normal way to run a JAR file would be to go into cmd (Windows) or a terminal (Mac & Linux) and use the command:
> java -jar {JAR_NAME}.jar {args}
where {JAR_NAME} is the file name of your JAR and {args} are any arguments that need to be defined up front.
If you want to not use a command line you could write a script that runs the JAR for you. Then you can double-click/run the script and it runs the JAR for you.
For Windows
I'd recommend writing a simple .bat or "batch" script that will run the file for you.
Keep the batch script in the same directory as your JAR file and just have it contain:
java -jar ./{JAR_NAME}.jar {args}
You could use a Powershell script or something similar though.
For Mac/Linux
As Mac (and Linux) are Unix systems we can use a .sh or "bash" script to run the JAR. Again keep the bash script in the same directory as your JAR file and use:
#!/bin/bash
java -jar ./{JAR_NAME}.jar {args}
.bat script is exclusive to Windows systems, .sh scripts are exclusive to Unix systems.
Because Windows and Unix are two very different systems, you're not going to find an executable file that works for all systems.
Converting your JAR into an executable
You could convert your JAR into a standard Mac "app" application bundle by following some instructions here.
However, I wouldn't recommend this as it isn't really necessary when you have a simple script/command option and for this method, you'd most likely need access to a MacOS machine with development tools.
I'm new at Java for Mac developement and I spent a lot of time finding a way how to create an installer. It's strange but it's not too much information about it. All the ways lead to using of JavaAppLauncher. (I use appbundler-1.0.jar and Ant to build .app) But I faced a following problem.
JavaAppLauncher sets working directory to user folder. My application consists of several jar files, resources and configuration files. All of them located in Contents/Java inside the .app structure. I use relative paths to them, relative to the executable jar with Main class.
I googled 2 discussions only, where someone asked how to set the working directory inside the .app bundle. Threre is a way to sed -Duser.dir JVM option in plist file of .app bundle. I tried this and it behaves strange. When I use "new File(".").getAbsolutePath()" - it shows the directory I pointed in -Duser.dir parameter, but when I try to create file on disk or read file from disk - it still goes to the user folder as it was by default.
May be someone knows what is wrong there?
Or is there any better way to create a java application bundle for Mac?
Thank you, guys.
Use JarSplice to create OS X app bundle. Compile all your jars and resources into one jar file and export as mac .app file.
Deploying Java Applications can be a pain if your application has multiple jars and native libraries. There are the options of using Java Web Start or Java Applets however these are not always suitable.
Executable Jars are an option, however if your project has multiple jars or native files then it can be tricky to use this option.
JarSplice attempts to solve this issue by allowing you to merge all your jars and native files into one, easy to use executable jar file.
Features:
Creates a single executable fat jar via an easy to use GUI.
Automatically extracts only the native files needed for the OS the jar is running on.
Automatically cleans up any extracted native files after the application quits.
Add VM arguments on jar creation which removes the need to enter them via the command line.
Optionally create a Shell Script (.sh) launcher for Linux with embedded fat jar.
Optionally create an OS X App Bundle (.app) for running your application.
Optionally create a Windows Application (*.exe) for running your application.
This is the best cross-platform installer creator i have found yet. You can get the java executable from here
Is there any .exe file like those VB creates for you which can be opened in different computers without any other app? created or can be created by Eclipse? I didn't find a thing in the project folder.
Java uses the "jar"-files as a container for executable binaries. But they require Java beeing installed on the target computer.
In Eclipse you can export you application via File -> Export -> Runnable JAR file
Then you have to select the entry point (a class with a main-method) which should be called on running the jar-file.
On the target computer you can normally run the jar file by double-clicking it (Java SE is required to be installed). When that doesn't work you can manually call "java -jar container.jar" to execute the jar in the Java enviroment.
Jar files are used for deployment but in order to run them you need the jvm.
You can convert jar files to .exe .
I'm not sure if the conversion will result in the app running on computers with different OS'es (probably not) than the one in which the the .exe program was created in.
Here are some references
How to create an exe file in java.
How can I create a Windows .exe(standalone executable) using Java/Eclipse?.
I have some code in Java using Eclipse and I would like to deploy it to unix envirnment. The program is simple console program that just takes some arguments at the run time, read a file and print out some results.
My question that what is best approach to deploy and run it in unix envirnment. I was just thinking to copy all the classes file to the unix envirnment and create a batch file to run the main class file. Does this sound okay? Or, should I create a runnable jar file?
Also, where should i put the jar files that the prgram is referencing (in classpath)?
Thanks
I think an executable jar file will solve your purpose here.
You should be able to execute it as
java -jar <jarfilename> <arguement1> <arguement2> .... <arguementN>
You can execute the jar file from the current directory itself, just make sure your jar file has executable permissions.
chmod +x <jarfilename>
ls -la
Designing for easy deployment is important in my opinion.
In our case, there are some components:
store project in the source code management system (git). we break down source code as
the developing source code to dev branch
the stable source code to release branch
use build tool, such as ant or maven, and provide a deploy script in the project. (we will talk deploy script in 3.).
provide deploy script to:
fetch the latest stable source code in the build server
build to executable files in the build server (whatever you do)
send the package to the target server
launcher (close the old app and run the new app) in the target server (via remote ssh command)
Currently, you think how to package the java, but it is a simple thing just about building and runing. When you talk about deployment, make it as easy as possible. Each time we deploy just to invoke the release script.
PS. I don't like the executable jar. Using un-packaging jars and compiled class can be sending by rsync very efficiently.
It sounds Ok and will work for you. Just one fix: you are going to write shell script for unix, not batch file.
But you can do better. Typically java classes are packaged into jar file. Jar file is just a zip file with optional META-INF, directory, MANIFEST.MF and other stuff. So it is better to package your application into jar and then run it as: java -cp yourjar.jar YourMainClass.
To create jar file you can use any tool that can create zip or utility jar that is a part of your JDK. You can also create automatic build using ant, maven, gradle, bildr etc that will help you to package your application.
I would do the following:
create a dedicated directory for this program. Copy the dependent .jar files to that directory
write a (short) script that sets the classpath to point to these jars and then executes the main class
Given the above, it's largely a matter of style as to whether you create a runnable .jar or not. It'll be hidden from the user.
I'm suggesting a script because:
you can set regularly used JVM parameters easily (memory options etc.)
it's a pain (and hardly intuitive) to type java -jar {pathtojar} etc.
By copying the jars to a dedicated directory, you can then use different versions of jars for different scripts (e.g. you may have 2 programs that use two different versions of commons-lang)
You should also (probably) use this script to explicitly determine which version of Java you use to run the program with. As you install/upgrade you don't want to break your programs and the scripts can be configured to explicitly tie down this info.
I find this quickest of all:
First, create a jar, copy to unix server and change file permission just as dopplesoldner mentioned below.
You can put your library classes and or jar dependencies in a lib folder
Then execute the jar
java -Djava.ext.dirs=lib/ -classpath yourJar.jar com.yourPackage.yourClass
yourClass will be the class having main(String args[]) method you wanted to execute.
I am not talking about the executable jar file, instead the .exe file for windows can that be generated for Java apps. I know there is some projects like JSmooth etc., so I am a little curious to know how they create exe's for jar or class files.
Why not write a .bat file (windows script) that launches the executable jars? The effect will be the same: user double-clicks and launches the app.