Set home or working directory - java

I have a java application and have bundled it as App.jar. There are some third party tools I have used (lets call it NumberGenerator). App.jar starts a process and calls NumberGenerator to get the output. To refer the executable, I have used relative paths new File("lib/NumberGenerator.exe") and it works all well.
Now on Mac, i have bundled the application using this and it automatically generates an application launcher. When I run by clicking at launcher, it launches the application. But it sets the home directory as ~ i.e. /Users/Jatin and not where the jar file was lying. Hence my application is unable to detect the lib folder (Obviously because it doesn't lie in that location)
In my Java code, how do I set the home folder as where my jar was lying?

return new File(MyClass.class.getProtectionDomain().getCodeSource().getLocation().getPath());
just check this thread How to get the path of a running JAR file?

I had a similar problem with linux. I don't know much about osx not AppBundler but in linux I solved it by creating this script
#!/usr/bin/env sh
java -Duser.dir=$(pwd) -jar myapp.jar
It may work if you manage to run the script by double clicking on it... see How to run a shell script in OS X by double-clicking?

I see two approaches:
In Info.plist, specify a java.library.path relative to $JAVAROOT, as described in the articles cite here.
<key>Java</key>
<dict>
...
<key>Properties</key>
<dict>
<key>java.library.path</key>
<string>$JAVAROOT/</string>
</dict>
...
</dict>
Use Java Web Start, which lets you manage native resources by OS and architecture.

You can try this:
System.setProperty("user.home", "your-dir");

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

Embedding a jre into WInrun4j only once for installer and application

For installation of my Windows desktop application I create an installer using izpack and then call it from an exe using winrun4j, also part of the izpack installation add another winrun4j wrapper for actually running the application once installed.
The trouble is that for the win4runj.exe to work it needs to be using the correct Java type, i.e if exe is 64bit it must run 64 bit jvm, if exe is 32 bit it must run 32bit jvm. This can be confusing for the user so I was going to take advantage of winrun4j to use an embedded jvm and then make two downloads available
widgetinstaller.exe (32-bit)
widgetinstaller64.exe (64-bit)
But my confusion is that for each download I only want to embed the jvm once but cannot work out how to do this because i have two exes (the installer, and the program itself) , can this be done ?
Update
Have it working if I install JVM twice (once included in install.jar when built with izpack, and once added to the zip file so can be used by widgetinstaller.exe).
widgetinstaller.exe resides in c:\code\widget\installer
Download 32bit jre from Oracle
Install into c:\code\widget\32bitJVM
Edit izpack.xml, adding <file src="C:/code/widget/JVM32bit" targetdir="$INSTALL_PATH"/>
Add for installer vm.location=32bitJVM/bin/client/JVM.dll to c:\code\widget\installer\widgetinstaller.ini
Add for widget itself vm.location=../32bitJVM/bin/client/JVM.dll to c:\code\widget\widget.ini
Rebuild code, and zip up including 32bitJVM in the zip file to create widgetinstaller.zip (this contains 32bitJVM, widgetinstaller.ini,widgetinstaller.exe, widgetinstaller.ico and install.jar)
Unzip and double-click on widgetinstaller.exe to install
But I only want JVM once, I know I need to remove the <file src="C:/code/widget/JVM32bit" targetdir="$INSTALL_PATH"/> from izpack.xml, but then how do I copy my 32bitJVM into program installation folder
that same JVM needs to be saved to the program folder.
Seems this has to be done outside of izpack itself ?
EDIT
Solution is to use Izpack loose packs described at at Can I install a file using Izpack 5 without it being part of the install.jar built by izpack
but it is quite confusing what must be set for the path, documentation incorrectly implies you put in a relative link http://izpack.org/documentation/installation-files.html#the-packs-element-packs.
Example
izpack install.xml is in C:\code\Widget\installer
<pack name="Base" loose="true" required="yes" preselected="yes">
<description>JVM</description>
<file src="C:\Code\Widget\JVM" targetdir="$INSTALL_PATH"/>
</pack>
When package up izpack installation should have:
JVM
install.jar
setup.exe
If the JVM is embedded in the installer then the program cannot use it.
And again if the JVM is embedded in the program then the installer cannot use it.
However you can still do it with only one JVM. This is done by coping the JVM to a local file.
So when you launch the installer with the winrun4j it will launch from the embedded JVM, then at the end of the installation, that same JVM needs to be saved to the program folder.
Your program winrun4j exe can then use the JVM that you saved to the folder to launch the program, you do this by setting the "vm.location" in winrun4j to the location of the saved JVM.
Process:
1) Run the installer exe with the required JVM
2) During the installation save the embedded JVM to a file
3) Once the instillation is complete the program exe can launch using the saved JVM

System Startup Folders/Methods on UNIX Platforms?

I have a Java application that needs to implement installation features of making a JAR launch on startup.
I can do this on Window by entering a REG file into the registry, but how can I do this on UNIX platforms? Linux and Mac if the methods are different.
Do Linux and Mac have system startup folders?
Remember that I need to do this programmatically not through system preferences or anything like that.
On Linux, the classic way would be through adding a script in the appropriate /etc/rcN.d/ directory (where N is a number 0-6 representing the 'run level'). I'm not sure whether that's still the recommended way, but it usually is still supported. This would also work with minor variations for other mainstream Unix variants (Solaris, HP-UX, AIX).
On Mac, you have to work harder. The files /etc/rc.common, /etc/rc.imaging and /etc/rc.netboot are related, but there are no /etc/rcN.d directories. There's also a script rc and another rc.local. Typing man rc reveals:
DESCRIPTION
rc.local is now unsupported and has been replaced with launchd(8), which bootstraps itself via the launchctl(1) bootstrap subcommand to read in launchd(8) jobs from the standard locations.
SEE ALSO
launchd(8), launchctl(1)
So, you should investigate launchctl and launchd, particularly launchctl.
This is how I would do it on ubuntu.
First create a bash script to run the java app, similar to.
#!/bin/bash
java -jar "helloworld.jar"
and save it, in this case called 'HELLOWORLD' in /etc/init.d.
Need to make the script executable so need to run
chmod +x HELLOWORLD
Finally to make it run on start up
update-rc.d HELLOWORLD defaults
On Macs I think its launchd, and on linux its init.d. They are config files.

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