Running a JavaFX from cmd - java

How can I make a .jar file from my JavaFX program without having to tell the computer where the modules are located:
javaw --module-path c:\devel\javafx\lib --add-modules javafx.controls,javafx.fxml -jar myProgram.jar
Let's say for example, that I wish to send the program to my friend, who doesn't have JavaFX libraries on his/her computer.
I just wanto make it work so, that when you write:
java -jar myProgram.jar
It opens

Why not go the full way and create a real platform installer as you would do it with any other software? Have a look at this tutorial which I co-authored (so I am biased): https://github.com/dlemmermann/JPackageScriptFX

Related

Gnome desktop file does not run "java -jar" command

I wrote an application with JavaFX and I would like to run it like a "native" app in Ubuntu.
So I created the following desktop file and put it in ~/.local/share/applications:
[Desktop Entry]
Name=Notes
Exec=java -jar /home/namhto/dev/Notes/build/libs/notes-1.0.0.jar
Icon=/home/namhto/dev/Notes/build/libs/icon.png
Type=Application
Terminal=false
The desktop icon is created but when launching it from the gnome desktop, nothing happens. Running the command java -jar /home/namhto/dev/Notes/build/libs/notes-1.0.0.jar from a terminal effectively starts my app.
I tried to mark the jar file as executable in Ubuntu but it does not change anything.
I would start with using /the/absolute/path/to/java for your exec command (which you can find out via which java for example), instead of a relative one. Maybe your ENV setup doesn't have java in $PATH somehow.
Anything else looks good to me.

Can you run a Java program in Linux using the name only, without the "java" command?

If I am writing HelloWorld, is there a way I can run the program from any directory by just typing HelloWorld? Sort of the same way once you set up Ant, you can just run Ant from any directory?
Just for some details, we are creating a CLI based toolkit for a customer, and just curious if we can compile it, install it, and just have them run it using the toolkit name.
You can always create a shell script, call it HelloWorld and make it run java with your JAR.
You'll then need to chmod the script to make it executable, and place it somewhere in your $PATH.
The script would like something like:
#!/bin/bash
cd /path/to/helloworld
java -jar HelloWorld.jar "$#"
or
#!/bin/bash
java -jar /path/to/helloworld/HelloWorld.jar "$#"
depending on your exact requirements.
Common solution for your problem is to create a separate launcher application, which is non-java application that runs your Java program. Launcher can be written in some compilable language such as C/C++ and compiled into native executable. Also it can be written in some interpreted language such as Unix shell, perl, python etc and made executable by adding #!/path/to/interpreter line at the beginning of launcher file and setting executable flag on it. Also there are several utilities that can generate launcher for your program such as launch4j or jsmooth.
On Linux (specifically), you could use the /proc filesystem (see proc(5) man page) and its binfmt_misc (actually the /proc/sys/fs/binfmt_misc/register pseudo-file and other pseudofiles under /proc/sys/fs/binfmt_misc/) to register java as the handler for .class or .jar files. Read the Documentation/binfmt_misc.txt file in the kernel source for gory details.
Then any executable .jar file would be interpreted by java (or jexec)
I'm not sure it is worth the effort. I find that wrapping your Java program in some shell script is much more easy (and more portable, because few Linux systems actually use binfmt_misc, and your customer may need some sysadmin skills to enable it).

Batch file to run a java app. How to port it in Mac?

I have a tiny batch file to kick off my java app:
its code is something as simple as
java -jar myapp.jar
(in reality it's slightly more complicated as we set a few properties)
A friend of mine has a mac. He has java installed on his mac.
But how do we run this batch from mac ?
Put the following code into a file and run chmod +x <filename> to make it executable.
#!/bin/sh
java -jar myapp.jar
If the Java app. is a desktop app. with a GUI, a good way to launch it is using Java Web Start. JWS provides the ability to set properties in the JNLP launch file.
JWS works on all desktop JREs. E.G. it will work on Windows, Mac. and *nix.

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 to make installable for java in linux

i have developed an application in java for linux i need to install it in linux in my system(ubuntu 10.04) and how to make executable for linux .i have the application as jar file
This and this.
echo "java -jar filename.jar" > runme.sh && chmod +x runme.sh
./runme.sh
You can create a simple shell script that run the following command:
java -jar mylib.jar
and then run the script using ./myscript.sh
If you're targeting a different machine from the one you've developed on, you'll need to package your JAR into a .deb file and either include a JRE/JDK in the distribution, or else declare a dependency on one of the apt packaged Java distros so that apt-get installs Java at the same time as your application.
For Java applications with a GUI, look into Java Web Start.
I never had much luck getting menu items to work on Ubuntu Linux, but desktop shortcuts were no problem. Maybe you can specify the exact sub-menu that the menu item is to appear under, and it will work.
JWS also works for Windows and Mac.
Follow this article and you need to create a file with gedit and name it with your package name.(e.g totem for totem media player listed in Applications Sound & Video) then in that file enter these lines and save to Input directory (mentioned in the above tutorial)
?package(yourpackagename):needs="X11" \
section="Applications/Sound" \
title="yourpackagename" \
command="/usr/bin/yourpackagename" \
icon="/usr/share/pixmaps/yourpackageicon"
enter your package name accordingly. Icon line is optional you can omit it. then
debian/input/yourpackagename /usr/share/menu/yourpackagename
add this line to install file
there we are adding the above created file into /usr/share/menu dir

Categories