Reputation: 371
i am using Linux ubuntu and i have created a java program named hola.java which is the following program code, this program works perfectly
import javax.swing.*;
import java.awt.*;
public class hola extends JFrame {
JButton b1 = new JButton("presionar");
hola(){
super("Botones");
setSize(250,250);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
FlowLayout flo=new FlowLayout();
setLayout(flo);
add(b1);
setVisible(true);
}
public static void main(String[] args)
{
hola bt = new hola();
}
}
This java program works perfectly when it runs Now I created a jar file of this program using in command line:
jar cf hola.jar hola.class
This creates a Jar file named hola.jar
I even wrote Main-Class: hola in the manifest.mf file.
When I try to run this by:
java -jar hola.jar
I get an error: An unexpected error occurred while trying to open file hola.jar
Please tell me how to run jar file so that I get an output :'( , what could be the possible reason that i can not run this program as a jar file, even the program works perfectly using "java hola.java"
Upvotes: 7
Views: 9760
Reputation: 226
Downgrade JDK version.
I am using docker to run my java app and got this problem.
At first, for debugging, use -Xdiag
It will tell you whether it is permission, file size or other problem.
java -Xdiag -jar hola.jar
I also attempted to increase the maximum file size that Java allows for jar signatures by setting the system property jdk.jar.maxSignatureFileSize:
-Djdk.jar.maxSignatureFileSize=800000000
However, this did not resolve my issue.
The solution was to downgrade my JDK to version 17.0.7., based on a discussions
https://github.com/whitesource/unified-agent-distribution/issues/48#issuecomment-1647660586
https://github.com/SAP/SapMachine/issues/1450
After downgrading, the application started working as expected in Docker.
Upvotes: 0
Reputation: 72680
Just to help people that meet the error nowdays (03/2024):
Error: An unexpected error occurred while trying to open file ...
This issue occurs while trying to launch a jar with Java version 1.8.0_375 and later versions. Due to a new property (jdk.jar.maxSignatureFileSize) introduced in this version.The default this is set as 8MB(8000000 bytes), however, my signed jar is be around 150MB+ in size.
So add :
-Djdk.jar.maxSignatureFileSize=800000000
Upvotes: 0
Reputation: 319
I experienced same error message attempting to launch via -jar:
$ java -jar app.jar
Error: An unexpected error occurred while trying to open file: app.jar
Root cause is that my sbt-assembly output .jar file contains more than 65535 entries.
$ zipinfo app.jar |wc -l
65543
Solution: Short term solution was removal of older dependencies to lower the number of assembled .class files below the 16 bit file count limit.
Long term solution will involve testing Zip64 jvm support on the target OS. Unsure of why the zip64 auto negotiation isn't occurring automatically.
This issue is reproducible using sbt-assembly 15.0, openjdk version "11.0.8" on MacOSX 10.15.7.
Start of the code review:
package java.util.zip;
...
public
class ZipOutputStream extends DeflaterOutputStream implements ZipConstants {
/**
* Whether to use ZIP64 for zip files with more than 64k entries.
* Until ZIP64 support in zip implementations is ubiquitous, this
* system property allows the creation of zip files which can be
* read by legacy zip implementations which tolerate "incorrect"
* total entry count fields, such as the ones in jdk6, and even
* some in jdk7.
*/
private static final boolean inhibitZip64 =
Boolean.parseBoolean(
GetPropertyAction.privilegedGetProperty("jdk.util.zip.inhibitZip64"));
Upvotes: 3
Reputation: 2333
This error mostly indicates invalid MANIFEST.MF
file. Perhaps long line, missing final line terminator, accidental empty line in the middle, ... many things can go wrong. Using -cp
just goes around the problem, but does not fix the root cause.
Upvotes: 3
Reputation: 2282
To run a java file within a jar file, you don't need to open it. You simply need to ensure your classpath is having the given jar file
If the class is within a package then you can run using
java -cp hola.jar package.hola
If the class is not in a package then simply use
java -cp hola.jar hola
If you are not within the directory where hola.jar is located, then you can try following:
In case of within package
java -cp /locationOfJar/hola.jar package.hola
or In case of not in package
java -cp /locationOfJar/hola.jar hola
Upvotes: 1