Make a .jar file that "just runs" everywhere? - java

I've written a few java applications (desktop, no browser involved) for friends, some of whom are less technical than I am. They use Windows, Linux and Macs. Originally I just used javac to generate bunches of .class files, zipped it all up and had them unpack it all. It works, but they had to install the JRE (terrifying for some), write a script to run java or javaw followed by command line args, app name, more arguments (incomprehensible for some), mark the script executable... I got mocked, somewhat rightfully, for a geek solution. They wanted to download from a website, doubleclick and be up and running.
I thought creating a .jar file would be the solution. It didn't help. In Eclipse, the options seem to be "create a jar file", which lets me include the handful of resources (.png files mostly) the apps needs, but the result isn't runnable from a command line. Or, create an executable .jar file, which doesn't seem (at least from Eclipse) to have a way to include resources - and the resulting .jar file doesn't start when double-clicked, even when I set the execute bit, even though the default run environment points to the Java suit. Even with a .jar file, I'm stuck with having them script "java -jar App...", and that's pretty much a dealbreaker.
I'm missing something. The point of Java is platform independence. Is there a platform independent way to have them download a single file, double click it and have it off and running?

If you are trying to create a runnable jar file from your project in Eclipse, you have to select that option when exporting. You can do this by Export > Java > Runnable Jar File. If you only select the Jar File option, you won't be able to run it.
If you want to wrap your application as a .exe, you can use launch4j. Here's a thread that explains it a little more and has some other options.

Related

How can Java run a PE32 when passed to its class path?

I've been wanting to reverse engineer this clients launcher in an effort to understand how the game was launching as a Java application despite it only having a PE32 executable alongside it.
The launching of the client goes as follows:
java -Xmx384M -Dfile.encoding="UTF-8" -cp TargetBinary.exe com.java.client.Client
Now I was curious what TargetBinary.exe actually was, as this was being ran on a *NIX system. Running file I observed this output.
TargetBinary.exe: PE32 executable (GUI) Intel 80386 (stripped to external PDB), for MS Windows
The second part is the com.java.client.Client after the TargetBinary.exe, this stands out as a Java pathing which Client is my target.
Here are my questions:
How can Java add the TargetBinary.exe to its classpath?
As a followup, are there any recommendations to decompile it to the point where I can observe Client and more so understand how it was all packed together?
Being a Portable Executable (PE) the libs, code, etc should all be there inside the TargetBinary.exe and somehow Java knows what to do with it?
Zip files are read from the back, so it's easy to put a zip (jar) file at the end of an executable file, and have it work as both.
See https://en.wikipedia.org/wiki/ZIP_(file_format)#Structure
Try running jar -tvf TargetBinary.exe to see the names of the classes and resources in the jar. If you make the file a dependency of a project in your IDE you can see decompiled code and navigate around the project, and run it and set breakpoints.

Java Jar file main class run when runs from command line, but not from Windows batch file

I'm re-using a standalone Swing-based Java class which backs up and restores mysql databases.
I've tested running it from a Windows batch file (.bat) on my dev system, and it works there.
But, if I run the batch file on a different Windows , I get a "main class not found" exception.
However, when I run the command directly on the command line, it works.
The command in the batch file to run it is:
java -cp lda-services.jar;bip-services-1.6.0.0-SNAPSHOT.jar;decryptor-1.6.0.0-SNAPSHOT.jar;slf4j-api-1.7.31.jar;commons-io-2.6.jar com.ilcore.util.SosaMaintenanceJFrame
The SosaMaintenanceJFrame class is contained in the lda-services jar.
Here's the error message:
Error: Could not find or load main class com.ilcore.util.SosaMaintenanceJFrame
Caused by: java.lang.ClassNotFoundException: com.ilcore.util.SosaMaintenanceJFrame
The class is definitely in the jar file, as I've extracted it the file and seen it.
Any thoughts on why this would be happening? I need to run inside a batch file so the user can just click on it to run it.
Most likely explanation
Your paths are relative, which means that the batch file isn't going to work unless you run it from the right place. In general, having a batch file that has an invisible rider stapled to it with: "I break in mysterious ways if not run from the appropriate dir" is a crappy batch file - make it better.
Better solution
Or, even better, get rid of it. You don't need batch files to distribute java programs.
Proper ways to distribute java programs:
The modern take is very very different from what you have here: JREs are dead, you must ship an installer that does the whole thing, notably including a java runtime (no longer called a JRE, and one you ship and keep up to date if relevant). That's perhaps a bridge too far for what you're doing here. Relevant tools include jlink.
A slightly less modern take involves jars with manifests:
Your jar file should contain a manifest. This manifest must contain 2 relevant entries:
Class-Path: lda-services.jar bip-services-1.6.0.0-SNAPSHOT.jar decryptor-1.6.0.0-SNAPSHOT.jar slf4j-api-1.7.31.jar commons-io-2.6.jar
and
Main-Class: com.ilcore.util.SosaMaintenanceJFrame
You can use jar's -m switch, or just include the manifest (it's just a file in the jar): it's at META_INF/MANIFEST.MF and it's a text file, each line is an entry, and an entry consists of a key: value pair.
When a jar contains this, just double clicking the jar and running java -jar thejar.jar will then take care of it all: Java will load the stated jars as part of the classpath, and these, crucially, are resolved as paths relative to the directory the jar is in, so it DOES work when you try to launch them from elsewhere, i.e. if you do:
C:
CD \
java -jar "c:\Program Files\MyApp\myapp.jar"
it works fine, whereas that batch script would fail due to being in the wrong place.
Build systems let you define the manifest too, check your build systems docs for how to do this, it'll be easy, and there are tons of tutorials if you search the web for e.g. 'manifest executable jar maven' or whatnot.
You can consider making a shaded jar. But I wouldn't.
A shaded jar takes all your dependencies and packs them into your main jar, so that there is only one jar. There is now no need for a Class-Path entry (the jar you run is obviously already on the classpath and there's nothing else to include) and your app is shipped as 'just' a single jar file.
But this is mostly a red herring: There are no consumer JREs anymore so you've made the user experience from a D- to a D. If you actually care about giving your users a nice experience, there's no getting around an installation process of some sort and once you have that, having the separate jars is no longer a problem. Separate jars are less hairy when signed jars are involved, are much easier to keep up to date, and have a significantly faster turnaround (when you build your stuff and want to ship what you built, shading takes ages, so it's nice to cut that step out). The faster your CI system tells you about failing tests, the better.
Meet in the middle
You don't have to upgrade to modules and the like. What you can do instead is use something like launch4j. The aim is to end up with a zip file along with the installation instructions: Make a dir somewhere. unzip this zip in it. Doubleclick 'myapp.exe'. Done.
The zip would contain an entire JRE, all your jar file deps, and your main app, and an exe file which launch4j made for you, that launches your app using the JRE packed into the jar. This means you know exactly which JRE is being used, and it'll work even on systems that didn't have one installed yet (which, these days, should be all of them - the notion of 'end user downloads a JRE from oracle and the user + oracle work together to keep that thing up to date and security-issue-free', is dead).
The fact that it's an EXE is nice: Now if the user e.g. alt+tabs through their apps, they get your app, with your name, and your icon, instead of 'javaw.exe' with an ugly coffee mug logo.
But when I try running it from the jar file generated by Maven, however, I get a "class not found" exception.
Even if you didn't get that error, you'd get another one unless you'd used Maven Shade, as that's the only way you're going to run that with a single jar. My guess as to why that particular error occurs is that the app class you're attempting to run is in fact in one of the *SNAPSHOT* jars

the .jar file does not execute when I click on it

I know this question would sound very stupid, but this is my first java program after all.
K, the question is, when type this: java -jar JavaApplicationTest.jar the program executes without problems, but when I go to the folder where the JavaApplicationTest.jar file is, and click on it, it does not execute. I thought that .jar files were like .exe file, are they? I mean, in the way that we click on them and the program runs; 'cos the java virtual machine is running in the back ground.
Please, any help would be very much appreciated.
No, they aren't exactly like .exe. Only .exe is an .exe.
When you execute a JAR file, it's the Java JVM that's running, not your JAR file. The JVM opens the JAR, loads the .class byte code, and executes the main class that you specified in the META-INF.
I'm guessing that your Windows operating system is doing something besides running the JVM when you double click. Try right clicking and seeing what options your Windows operating system presents to you. If one of them is to unzip the file, you'll have to add running the JVM as another choice.
The jar-file runs. But there's a big difference between:
java -jar someJar and someJar.jar/respectively double-clicking on the jar.
The full command launches the jar in the same commandline, in which you have entered the command. Double-clicking on the jar creates a process aswell. But with a completely separate console-window that is hidden.
Java archive or jar is an archive of compiled java byte code and resources which can be run on a java virtual machine. ".exe" is a windows extension for directly executable code mostly used by installers or programs that do not need to be installed.

How to get location of runnable .jar File when run from GNOME

I am developing a small Java application using Swing, that is supposed to run on Windows/Linux and MacOS.
Eventually it will ship as a runnable jar with some config files in the same folder. To load them I need the path to the folder the jar is stored in within the program.
There are already a couple of threads like this one or this one.
My problem is, that all the solutions discussed there work fine, when I run the program from within eclipse or call the runnable jar from a terminal like so:
java -jar /path/to/jar/jarfile.jar
However when I click on the jar file in Cinnamon or Gnome (which is what most of the users will know to do), I do not get the correct paths. (MacOS users report the same issue)
Here is what I've tried so far and what the output is when run via double click (all those display the desired path when run from eclipse or a terminal):
ClassLoader.getSystemClassLoader().getResource(".").getPath()
Output: file:/usr/lib/jvm/java-6-openjdk-common/jre/lib/ext/pulse-java.jar!/
SomeClass.class.getProtectionDomain().getCodeSource().getLocation().getPath();
Output: ./
System.getProperty("user.dir");
Output: /home/myusername
Is there any other way to do it or am I doing something wrong when exporting the jar? Any help would be appreciated!
Cheers
Nick
Make it simple, and use a startup script (.bat/.sh file) to run your application. This startup script will get the path of its own location in the filesystem, and pass it as an argument or system property to the Java application. This has the additional advantage of being able to pass other arguments, like the size of the heap, etc.
On windows, %~dp0 is the path of the directory containing the executed bat file. On Unix, you can use $(dirname $0).
You could store all config files as resources in a jar, and copy them to files in home.dir + ".AppName/".
Painful as it is, the Preferences API, Preferences.systemNodeForPackage, seems the wisest alternative, if there is little structured config data. There is an inputStream method for import; your initial config template could be a resource in the jar.
Just get the class path using System.getProperty("java.class.path") and scan it for your ".jar" name. Note that path separators are OS dependent (File.pathSeparator)

Can my Java software be transformed to .exe software?

Without learning new programing languages, can we using Java get .exe (executable windows file) software directly? And is there away to make .jar (Java ARchive) software transform to.exe (executable windows file)?
Would this conversion effect the performance of the software?
One of the important points of Java is that it'll run on any platform (e.g. Windows, Linux, etc) that has a suitable Java Virtual Machine (JVM) installed. A .exe file is compiled to work on only one platform.
What you think you want is to turn a .jar into an .exe so you can launch it easily. What I expect you really want is just an easy way of launching a Java app that anybody can understand, including your parents.
The easiest way of doing this is to create a Windows batch file that launches your Java app. One of line of script, one new file, one new instruction to your users - "Double click on runme.bat"
There are ways of turning a .jar into an .exe, but you should think about whether that's really what you want, and if so, why.
Note: you can launch a .jar in Windows by just double clicking on it, as long as the main class is specified in the manifest. You might want to look into tools like Apache Ant to simplify building .jars and their manifests. Just a thought.
EDIT:
A batch file in Windows is a simple text file that contains commands. You can test the commands by running them in the command prompt (Start -> Run -> cmd). When you run a batch file the commands in it are just fed to the command prompt one at a time.
How to run a Jar from the command prompt: "java -jar myfile.jar"
If you create a batch file (.bat - use Notepad or your favourite text editor) containing "java -jar myfile.jar" (without the quotes!) then double-clicking it will launch the main class specified in the manifest of myfile.jar. myfile.jar has to be in the same folder as the batch file, though.
If you use the command "java -jar lib\myfile.jar" (again, without the quotes) then your batch file will run myfile.jar, which needs to be in a folder called lib that's in the same folder as the batch file. Use this approach when you have a whole load of Jars with your application and you don't want to shove them in the user's face :)
Note that moving the batch file will break it, unless it uses absolute paths to the jar file - e.g. "java -jar C:\dev\myfile.jar". Of course, if you use absolute paths then moving the Jar will break the batch file anyway :)
Also, note that you should be able to run a Jar file just by doubling clicking on it in Windows, as long as the main class is specified in the manifest. Give it a try. Of course, convincing your users that they can double click on it is another matter entirely...
As a final note, if you use a batch file, your users will get a nice command prompt window sitting in the background until your Java app closes. That is, unless you start your batch file command with "start". E.g. "start java -jar myfile.jar". If you have your app configured to log to System.out or System.err, you will still get a command prompt when your app writes to either of those streams, though.
Final final note, in the Linux world the equivalent of batch files are shell scripts.
You can launch a .exe from java with Runtime.exec() or ProcessBuilder.
You can make a .exe launcher with http://launch4j.sourceforge.net/
Yes, it is possible: http://www.excelsior-usa.com/articles/java-to-exe.html
I'm not sure if this is an appropriate answer because I don't work with Java but could you not using a shortcut file pointing to the java runtime and pass the jar as an argument, this
I realise this is not the only way, nor probably the best way but to me it seams a little better than creating a batch or converting to an exe file if all you're looking for is a convienient way to launch a java app
The very simplest way is to write a windows batch (.bat) file which will start the application.
I was using a wrapper for java to make exe files for a pretty long time:
JStart32
It is just a wrapper for *.jar files.
But ask yourself:
Wouldn't an exe kill the purpose of javas platform independency?
Check out jsmooth (free) and exe4j and you might want to read make your swing app go native

Categories