Executable Jar file not working another machine - java

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

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

Running .jar File Java 8

I've run .jar files before, but I've encountered a "different" situation, and I'm not sure what to do. I'd appreciate if someone could help me out.
Previously, I programmed with Java 6 and Eclipse Juno exported all my programs to runnable jar files. I'd get a .jar file that I could run by just double clicking on it. The files always looked something like this (note the jar file icon):
Recently, I wrote a program in Java 8 with Eclipse Luna (Release 4.4.0) and exported it to a runnable jar file, and I got something different (note the different file icon):
It no longer runs when I double click it. Instead, my computer uncompresses the jar, as it would a zip file. I tried running it from terminal. I cd'd to the directory and typed
java -jar graph3D.jar
I got the following error message:
Error: Unable to access jarfile graph3D.jar
After uncompressing the jar file, I found a folder named META-INF with the manifest file, MANIFEST.MF in it. It was the only file that seemed to resemble an executable file. Do I have to do something with that?
Could someone explain how I can run the second jar file graph3D.jar? Is it something new with Java 8, or something different about Eclipse Luna, or something else?
(Both programs run fine in Eclipse, by the way)
Thanks for your time and help.
Edit:
Below was the dialog box Eclipse displayed if anyone is interested.
Selecting "Use .jar;.zip" makes the filename "graph3D.jar;.jar;*.zip" .
Selecting "Use .zip" makes the filename "graph3D.jar;*.zip"
Selecting "Cancel" doesn't let you go forward.
You'd have to manually delete the extra file extension.
Somehow when you exported the file, the filters for the file dialog box (*.jar;*.zip) got attached to the filename, which is graph3D.jar;*.jar;*.zip, not graph3D.jar. Java can't find it because it doesn't have the name you supplied. Rename the file and pay close attention next time you export; either you fat-fingered something, or you're triggering a significant bug that needs fixing.
I recommend that you will access the build folder after you've built your project on the IDE under your project folder (in your workspace) and copy both the libraries folder and the .jar and post them wherever you want the program to be "installed", you'll then have an executable jar that should run smoothly without problems, just as I said don't forget the lib folder.
I think there is nothing new in Java 8 related with the running jar, I guess you need to check the the Eclipse export issues, it seems your classes are missing from your second jar file.

I can't run JAR file?

I have created a Java JAR file in NetBeans. I have already run clean and build on the program and everything was successful. When I run the program in NetBeans is also successful, but when i locate the JAR file in the NetBeans dist folder and run it, nothing happens. The program doesn't run, and there are no exceptions or messages. Could you please help me understand why.
EDIT: I already solved the problem.
It sounds like you're probably on Windows :)
One way to "run" a .jar file is to create a Windows File association for ".jar" suffix and the "javaw.exe" command:
1) Go into Windows Explorer and [Browse...] to your .jar file
2) Right-click the .jar file, and select the "open with..." option.
3) Select javaw.exe as above, and see if it runs.
Are you trying to make executable jar ? You might wanna put your main class in manifest of the jar file.
Something like :
Main-Class : file_name_with_main_method
I would suggest checking the Console if you are on a Mac. You can view errors from the Jar launch to understand why it failed.

Executing .jar files with a double click

I've been using Eclipse as my Java IDE and today I ran into an interesting problem. I used the built in file>export>runnable jar file to create a jar file for one of my programs. It runs perfectly if I start it from the command line/with a batch file, but doesn't run from a double click. I made absolutely certain that .jar files are associated with javaw and it still didn't work. It isn't a huge problem I absolutely NEED fixed, but if anyone has any ideas I'd love to hear them.
Update: I tried using Aram Kocharyan's solution (see answer below). No luck. It seems to be just this one .jar file too. All other jars launch with a double click. Rebuilding the jar doesn't help. :P
I wrote a short guide on this a while back you might find helpful:
http://ak.net84.net/projects/how-to-make-a-multi-platform-executable-java-jar-file/
This will work on double click in windows without setting any additional settings, and I've tested it with the Jar Launcher on Mac and it doesn't complain.
open your jar with..javaw
which is in bin folder of jre... in my computer it is #
C:\Program Files\Java\jre\bin\javaw.exe
Put this inside a .bat:
start javaw -classpath "%~dp0YOU-JAR-NAME.jar" -Djava.library.path="native" foo.package.bar.YourClassWithMainMethod
the option -Djava.library.path="native" its not required for run, i include this because one reason for using a .bat instead do a double click on a .jar is to use JVM-parameters, in my case i need this parameter for my project run.
the %~dp0 part get current directory

Categories