Execute a .app file from within a java program - java

I need to execute an external program from within my java application. I'm programming on a mac and have a .app application I'd like to run when the user selects it. It runs successfully on windows using:
String cmd = "path_to_executable\program.exe\"";
Process p = Runtime.getRuntime().exec(cmd);
...
But this does not work with an .app file. I've opened the contents of the .app file and have found a Unix executable file along with a other supporting files for the appliation. Is the unix executable file equivalent to an .exe file ?

Launching an Mac application from Java on OS X is just a matter of invoking the built-in open command with the Application as a parameter. The open command line tool in OS X knows how to correctly open many different file types, including application bundles. If you were trying to launch the TextEdit.app application, you would invoke:
open /Applications/TextEdit.app
In Java, you would use:
String cmd = "open /Application/TextEdit.app";
Process p = Runtime.getRuntime().exec(cmd);

Yes, the executable that you should be running is the unix executable located in the .app bundle. It might not work properly though, you might need to cd into the directory or something.
Most application binaries are located in Contents/MacOS/App Name. You could make a method which would take in a string name and execute that binary based on the standard directory.

A .app file is really just a folder. It is a way to self contain applications on a Mac. The actual binary file is, most likely, the Unix executable you found. It would help to know some details about the app.
The binary files for a .app are located in Application.app/Contents/MacOS

Related

How do I make a code that will make Java code/file to an .exe file [duplicate]

I want to convert a .jar to an .exe for microsoft. Is there any program converter for this?
Also if there's one for Mac and Linux I would appreciate suggestions for those too.
Launch4j works on both Windows and Linux/Mac. But if you're running Linux/Mac, there is a way to embed your jar into a shell script that performs the autolaunch for you, so you have only one runnable file:
exestub.sh:
#!/bin/sh
MYSELF=`which "$0" 2>/dev/null`
[ $? -gt 0 -a -f "$0" ] && MYSELF="./$0"
JAVA_OPT=""
PROG_OPT=""
# Parse options to determine which ones are for Java and which ones are for the Program
while [ $# -gt 0 ] ; do
case $1 in
-Xm*) JAVA_OPT="$JAVA_OPT $1" ;;
-D*) JAVA_OPT="$JAVA_OPT $1" ;;
*) PROG_OPT="$PROG_OPT $1" ;;
esac
shift
done
exec java $JAVA_OPT -jar $MYSELF $PROG_OPT
Then you create your runnable file from your jar:
$ cat exestub.sh myrunnablejar.jar > myrunnable
$ chmod +x myrunnable
It works the same way launch4j works: because a jar has a zip format, which header is located at the end of the file. You can have any header you want (either binary executable or, like here, shell script) and run java -jar <myexe>, as <myexe> is a valid zip/jar file.
JSmooth .exe wrapper
JSmooth is a Java Executable Wrapper. It creates native Windows launchers (standard .exe) for your Java applications. It makes java deployment much smoother and user-friendly, as it is able to find any installed Java VM by itself. When no VM is available, the wrapper can automatically download and install a suitable JVM, or simply display a message or redirect the user to a website.
JSmooth provides a variety of wrappers for your java application, each of them having their own behavior: Choose your flavor!
Download: http://jsmooth.sourceforge.net/
JarToExe 1.8
Jar2Exe is a tool to convert jar files into exe files.
Following are the main features as describe on their website:
Can generate “Console”, “Windows GUI”, “Windows Service” three types of .exe files.
Generated .exe files can add program icons and version information.
Generated .exe files can encrypt and protect java programs, no temporary files will be generated when the program runs.
Generated .exe files provide system tray icon support.
Generated .exe files provide record system event log support.
Generated windows service .exe files are able to install/uninstall itself, and support service pause/continue.
New release of x64 version, can create 64 bits executives. (May 18,
2008)
Both wizard mode and command line mode supported. (May 18, 2008)
Download: http://www.brothersoft.com/jartoexe-75019.html
Executor
Package your Java application as a jar, and Executor will turn the jar into a Windows .exe file, indistinguishable from a native application. Simply double-clicking the .exe file will invoke the Java Runtime Environment and launch your application.
If your program is "publicly available non-commercial in nature" and has "a publicly available Web site that meets the basic quality standards", then you can try and get a free license of Excelsior. If its not then it's expensive, but still a viable option.
Program: https://www.excelsiorjet.com
As a side note: Here's a study of all existing Jar to EXE programs, which is a bit depressing - https://www.excelsior-usa.com/articles/java-to-exe.html
Despite this being against the general SO policy on these matters, this seems to be what the OP genuinely wants:
http://www.google.com/search?btnG=1&pws=0&q=java+executable+wrapper
If you'd like, you could also try creating the appropriate batch or script file containing the single line:
java -jar MyJar.jar
Or in many cases on windows just double clicking the executable jar.
I used Launch4J and it works flawlessy,
First, Go to your jdk and copy the bin and lib folder
Then create a folder for your app and make a folder called jre-(version),
Then paste these inside
Then open launch4j Put the exe file's path (Inside the app's folder)
Then go the jre option and inside bundled paths type jre-(version)
Then in minimun version type the version of the java you coded it in.
Then in the jre option put "Only use private jdk runtimes"
Then hit the gear icon. It will open a file chooser.
Choose your application's folder, done
Now open the exe file generated. It should work.
In case it does not, try the test run button (The green play one). If it says LinkageError in the console below, then in the jvm-options type --enable-preview. It should work.
I hope it helped.
For Windows, you can convert jar to exe using following ways:
Using Netbeans
https://erainnovator.com/convert-jar-to-exe-file/
Using Excelsior JET
https://youtu.be/iQSfUb8chjg
if you need to convert from .jar to .exe from java 14 you can used jpackage
jpackage is a command-line tool to create native installers and
packages for Java applications.
this article will help you

Can't open a java file in terminal

I have java installed but when I try to open it in terminal it doesn't work
I type
java -jar Minecraft.jar
the output I get is
unable to get access to jar file Minecraft.jar
How I could fix this so I can next time just click the app. My operating system is Linux
Minecraft.jar file must be in your current working directory, which seems not being the case. So you either change your working directory to where Minecraft.jar resides or give full path to it
java --jar /your/valid/full/path/to/Minecraft.jar
give the full path to jar or go to directory and run command java -jar Minecraft.jar.

How to start a java jar with administrators privileges on windows 7

I am developing a grammar with ANTLRWorks on Windows 7. The ANTLRWorks debugger doesn't start unless ANTLRWorks has Administrator privileges. ANTLRWorks is delivered as one Java .jar which starts by a doubleclick on the file.
Question: How can I assure that the .jar file runs with Administrator Privileges ?
You can run cmd under Administrator and launch ANTRLWorks using java -jar command.
You may want to have a look at Johannes Passing's Opensource Tool Elevate.exe There are many possibilities of using it. You can make a wrapper jar or simply use a batch file with something like:
Elevate.exe javaw.exe -jar path-to-your-jar-file
and this will launch UAC prompt, when allowed will permit the jar to have elevated privileges.
if you do not like batch files, then you can write a simple wrapper class to do so. Normally i make another jar file as a launcher for the jar which requires admin access, and in the launcher file, simply execute external process with the above command, and this way my jar has admin privileges.
first, download this program from here
http://code.kliu.org/misc/elevate/
then copy the "elevate.exe" file to the folder where your .jar file is.
Make, a .bat file as "elevate java -jar YourJARFile.jar".
Run the .bat file.Simple as that.

Deploying a Java project on Linux system

I developed a project using Java and now I've to deliver it to client who is using Linux. Which executable file format will be delivered and how to make that?
Executable file format?
If you're delivering a Java app, give them a jar file (and associated libs).
Provide a shell script to set up its environment and execute it.
For example, assuming I define ROOT_DIR as my app's install directory, and so on:
CLASSPATH="${ADD_JARS}:${CLASSPATH}:${ROOT_DIR}/lib/myApp.jar:\
${ROOT_DIR}/lib/jibx/jibx-run.jar:\
${ROOT_DIR}/lib/jibx/xpp3.jar:\
${ROOT_DIR}/lib/bindings.jar:\
${ROOT_DIR}/lib/commons-lang-2.0.jar:\
${ROOT_DIR}/lib/forms-1.0.5.jar"
"${JAVACMD}" -Xmx256M -DanyDefsNeeded=foobar -Dbase.dir="${ROOT_DIR}" -cp "${CLASSPATH}" myApp.main.Launcher "$#"
What goes into the shell script depends totally on what your app actually needs to start up.
A jar. If it is not executable, then a script (.sh) to launch the jar.
Well basically what you wanna put in a .sh file is the commands you'd normally type at the console to run your jar file. They should be separated by a new line (i.e. each on a separate line in the .sh file).
The most basic you can go is add something like this to your sh file:
java -Xms=64m -Xmx=256m -jar myJar.jar -classpath [dependencies dir]/dep1.jar,[dependencies dir]/dep2.jar
beyond this you can do more exotic stuff, like parametrise some environment variables, get command line argumens from when the .sh is launched and pass them to the jar executatble etc. Look up "bash scripting" for advanced stuff:
http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-2.html#ss2.1
You might have better luck using Launch4J, IzPack or other installer that has cross-platform capabilities. This might be a better first option than trying to understand the intricacies and idiosyncrasies of the different Linux distributions and shells.
If your app. has a GUI, the best user experience for installation/deployment can be had by way of Java Web Start. Note that JWS can deploy apps. to Windows, *nix and Mac. and avoids all the maintenance woes of generating 3 separate (platform specific) executables.

How can I convert a .jar to an .exe?

I want to convert a .jar to an .exe for microsoft. Is there any program converter for this?
Also if there's one for Mac and Linux I would appreciate suggestions for those too.
Launch4j works on both Windows and Linux/Mac. But if you're running Linux/Mac, there is a way to embed your jar into a shell script that performs the autolaunch for you, so you have only one runnable file:
exestub.sh:
#!/bin/sh
MYSELF=`which "$0" 2>/dev/null`
[ $? -gt 0 -a -f "$0" ] && MYSELF="./$0"
JAVA_OPT=""
PROG_OPT=""
# Parse options to determine which ones are for Java and which ones are for the Program
while [ $# -gt 0 ] ; do
case $1 in
-Xm*) JAVA_OPT="$JAVA_OPT $1" ;;
-D*) JAVA_OPT="$JAVA_OPT $1" ;;
*) PROG_OPT="$PROG_OPT $1" ;;
esac
shift
done
exec java $JAVA_OPT -jar $MYSELF $PROG_OPT
Then you create your runnable file from your jar:
$ cat exestub.sh myrunnablejar.jar > myrunnable
$ chmod +x myrunnable
It works the same way launch4j works: because a jar has a zip format, which header is located at the end of the file. You can have any header you want (either binary executable or, like here, shell script) and run java -jar <myexe>, as <myexe> is a valid zip/jar file.
JSmooth .exe wrapper
JSmooth is a Java Executable Wrapper. It creates native Windows launchers (standard .exe) for your Java applications. It makes java deployment much smoother and user-friendly, as it is able to find any installed Java VM by itself. When no VM is available, the wrapper can automatically download and install a suitable JVM, or simply display a message or redirect the user to a website.
JSmooth provides a variety of wrappers for your java application, each of them having their own behavior: Choose your flavor!
Download: http://jsmooth.sourceforge.net/
JarToExe 1.8
Jar2Exe is a tool to convert jar files into exe files.
Following are the main features as describe on their website:
Can generate “Console”, “Windows GUI”, “Windows Service” three types of .exe files.
Generated .exe files can add program icons and version information.
Generated .exe files can encrypt and protect java programs, no temporary files will be generated when the program runs.
Generated .exe files provide system tray icon support.
Generated .exe files provide record system event log support.
Generated windows service .exe files are able to install/uninstall itself, and support service pause/continue.
New release of x64 version, can create 64 bits executives. (May 18,
2008)
Both wizard mode and command line mode supported. (May 18, 2008)
Download: http://www.brothersoft.com/jartoexe-75019.html
Executor
Package your Java application as a jar, and Executor will turn the jar into a Windows .exe file, indistinguishable from a native application. Simply double-clicking the .exe file will invoke the Java Runtime Environment and launch your application.
If your program is "publicly available non-commercial in nature" and has "a publicly available Web site that meets the basic quality standards", then you can try and get a free license of Excelsior. If its not then it's expensive, but still a viable option.
Program: https://www.excelsiorjet.com
As a side note: Here's a study of all existing Jar to EXE programs, which is a bit depressing - https://www.excelsior-usa.com/articles/java-to-exe.html
Despite this being against the general SO policy on these matters, this seems to be what the OP genuinely wants:
http://www.google.com/search?btnG=1&pws=0&q=java+executable+wrapper
If you'd like, you could also try creating the appropriate batch or script file containing the single line:
java -jar MyJar.jar
Or in many cases on windows just double clicking the executable jar.
I used Launch4J and it works flawlessy,
First, Go to your jdk and copy the bin and lib folder
Then create a folder for your app and make a folder called jre-(version),
Then paste these inside
Then open launch4j Put the exe file's path (Inside the app's folder)
Then go the jre option and inside bundled paths type jre-(version)
Then in minimun version type the version of the java you coded it in.
Then in the jre option put "Only use private jdk runtimes"
Then hit the gear icon. It will open a file chooser.
Choose your application's folder, done
Now open the exe file generated. It should work.
In case it does not, try the test run button (The green play one). If it says LinkageError in the console below, then in the jvm-options type --enable-preview. It should work.
I hope it helped.
For Windows, you can convert jar to exe using following ways:
Using Netbeans
https://erainnovator.com/convert-jar-to-exe-file/
Using Excelsior JET
https://youtu.be/iQSfUb8chjg
if you need to convert from .jar to .exe from java 14 you can used jpackage
jpackage is a command-line tool to create native installers and
packages for Java applications.
this article will help you

Categories