Making a Java application for Mac users - java

I succesfully generated a dmg file thanks to this question. My issue now is that when I open the .app file, the program starts and immediately stops.
1) On Mac, how can I get the java error if any?
2) I think that the error occurs because the .app cannot access the files that are within the dmg. The same answer indicates that I could add my files to the .app file. I can do that, but then, how am I supposed to access these files from my application? What would be the path I should use?
3) For GNU/Linux and Windows users, I currently have a .jar file that updates my application by updating the myapp.jar file and some other files. If everything is packaged into the myapp.jar file, how am I supposed to do that?
Thanks for you help!

Have not tested the following but based on what’s described at Bundle Java program for Mac users with maven from GNU/Linux, the following are the steps I would try.
Open Terminal and cd to whatever directory .app is in.
Run find . -name java.
That will return something like foo/bar/jre1.8.0_112.jre/bin/java I guess.
Run find . -name myapp.jar where myapp.jar is the name of the jar you made.
That will return something like foo/bar/myapp.jar.
Run foo/bar/jre1.8.0_112.jre/bin/java foo/bar/myapp.jar.
If that runs, some error message will get emitted that should explain what else is not working.
Note that you can change files inside that .app directory; specifically, you can replace the jar file within the directory.

Related

How to make my jar file executable by double click to open terminal

Okay so basically, every time I've seen this question asked it doesn't have the answer I'm looking for which is why I'm trying to ask myself.
Basically, I made a java project on BlueJ (required by school, sadly. But I can export it elsewhere if that's the problem (preferably IntelliJ or Visual Studio Code)), but essentially, I want to make it so I can simply double click my .jar export and it will open a terminal window and launch. When I try this however, I get this error: First Error
And when I click Ok then this appears:
Second Error
Now, if I want the jar to run on terminal, doing java -jar jarFileName.jar works perfect, but what I want is for that to happen when I double click the jar file; for it to launch and run from the terminal.
All the other answers I've seen for this problem blame the users computer and usually tell them to install some version of java or whatever, but this isn't the problem. I have other jar files that I can run perfectly fine, like Minecraft Forge installer or Minecraft Spigot installers (sorry only examples are Minecraft, not much else uses Java). And also this was for a uni project and no one else in my field know how to get their jar to be executable, nor does this file work for anyone else.
So yeah, I know it's a problem with how the jar was compiled or something and not what java is installed my PC.
I just want to make it double-clickable for the convenience of when I send it to friends, but if anyone knows another way I can get around this problem then that's fine. For example I know I could make a Bash file which simply does the java -jar jarFileName.jar for me or something, but I'd rather have it all as one jar file, so I don't know if this can be incorporated into the file but yeah.
Also I'd really like a solution which doesn't involve downloading external programs or whatever, since all I want to do is send this file to my friends and have them just double click it to launch.
TL;DR, what do I have to do to make my jar file actually executable by double clicking it. Thank you.
To make a jar file executable you have to set the entrypoint
You can do this by passing the e option to the jar command when you create the jar file, for example:
jar cvfe myapp.jar com.mycompany.myapp.MyApp com\mycompany\myapp
This will add the Main-Class entry in the jar's manifest pointing to the startup class, which should have a main method, for example:
package com.mycompany.myapp;
public class MyApp {
public static void main(String[] args) {
// start up the application
}
}
When you create a JAR file, it automatically receives a default manifest file. There can be only one manifest file in an archive, and it always has the pathname
META-INF/MANIFEST.MF
The entrypoint is specified by the Main-Class, for example:
Manifest-Version: 1.0
Created-By: 1.8.13_37 (Oracle Corporation)
Main-Class: com.mycompany.myapp.MyApp
If you want to make an existing jar file executable you could unzip it, modify the manifest and zip it again (and rename it so it has the .jar extension - a jar is just a zip).
But I think the errors you show are not caused by the jar not being executable, but by some environment issue, causing the Java application to not find the necessary variables or paths to make the Java Native Integration (JNI) work. To solve this you would have to provide more detail on what you are trying to do with JNI.

how to run .jar files on Synology

for a Synology NAS DS118 i want to run a jar file and i'm looking for a way to hibernate the program on the synology so that it executes automatically, can anyone help ?
i have tried to run the .jar from the ssh client but i think i should first make the jar file executable and i still didn't figure it out...i couldn't find any detailed tutorials yet :( –
You have to installed and updated the Oracle Java package. You have to also enable terminal access to the NAS and navigated to the right directory where the jar file is. I am not aware of your specific package, but i assume since you can start the foo.jar via double click you might also be able to start this foo.jar on your DiskStation like this :
ssh -ladmin YOURDISKSTATIONIP
YOUR_ADMINPASSWORD
cd TOYOURJARDIRECTORY
java -jar foo.jar
where foo.jar is the .jar you want to start.
You can write a bash service and it gonna be automatized. I hope it will be helpful for you.
To make your java file executable. See this post;
https://stackoverflow.com/a/47209860/5879876

Creation of a .bat file of a java application

I am a bit confused about the process to create a .bat file of a java application. I have exported the executable jar using IDE say Application.jar in C: directory. Then I have written two lines in a .txt file as stated below and saved it as .bat file in the same directory where i have my application.jar. But on double click of the .bat file, the application is not getting executed.
.BAT file code
javac Application.java
java -cp . Application
Note: I have also set the JRE and JDK path in my environment variables till bin path in My Computer properties. But it is not working. Can someone suggest me how can I fix this, because I want to execute my code by doubleclickng on a .bat file. It will be nice if someone can provide me every step I need to follow to accomplish this as I havent ever done this before.
Thanks ,
The first line in your batch file is attempting to compile your program !?
The second line is attempting to run the Application.class file.
What you want if you have produced an executable jar file is:
java -jar Application.jar
But you don't really need the batch file at all. If you double click on the jar file and it runs your program then you can just create a shortcut to it.
Your .bat is just fine. When you double click it might be executing and then closes. This is because your program might not have any UI and it isnt waiting for any input. To verify this take a command prompt and then execute your bat file via that.
In other case I assume that you have a java class called Application and you need to run this via a batch file. In that case if the class have a main method then you just need one line in .bat file
java -cp <the path to class file> Application
So you might be using a javac just to take advantage of class path as current directory. So when you say
javac Application.java
java -cp . Application
It compiles the class to current folder and set that as class path and then execute. This is absolutely file as long as the Application.java doesnt have any third party dependency. But in this case again you need not set -cp to . (current directory will be taken as classpath automatically unless otherwise specificed). So below will also work fine.
javac Application.java
java Application
I support Jurgen reply. If you have an executable jar file and a jre in path then double clicking it will run the application. The META-INF folder inside the jar will have a MANIFEST.MF file which uses a property called Main-Class: to specify the main executing class. And on double clicking this class gets executed. However its only useful if you have a UI. Else it'll also have no effect.
In all these context the Application.jar you mentioned is irrelevant. If that is a third party jar that you need to run the you should include that in -cp argument.

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)

Executable Jar file not working another machine

I have created an executable jar file to my java programm using Eclipse(File->Export).
When i double click the jar it works fine.After i copy the jar file to another machine(windows) i got an error "Invalid or corrupt jarfile".I have installed JRE and JVM.
Anybody know what is the issue.
Help is highly appreciated.
Thanks,
vks
as you said you have double click
try command line as another machine(windows) might not be assign jar extension to execute.
to run jar manually do.
java -jar example.jar

Categories