Extracting .jar from .jar.zip - java

I have downloaded a zipped jar file (the jar file got zipped automaticly, there is no option not to do so).
Now I have thatLib.jar.zip
I want to 'java -jar thatLib.jar', but when i unzip the file i get a directory instead of a .jar.
I still tried to run it and got
unable to access jar file
on Windows and
Invalid or corrupt jar file
on Ubuntu.
I think the problem lies within the extraction

Related

Why I can't run JAR File in another position in my computer?

I have created a JAR file, it is in "out" folder of my project, at this time the format of this file is Executable Jar File, but when I copy this file to another folder it cannot run and it says error unable to access jar file, now the format of the file is JAR file but it isn't executable jar file.
I hope to be able to keep the format of the file as executable jar file so that it can be executed on another machine at any time.
I hope someone can help me with this error and explain why it is giving me this error.

Desktop.getDesktop().open(file) doesn´t work if jar needs an external file

I hava a jar file file.jar that needs a folder with pictures next to where the jar is. The program loads the pictures and shows them.
It works fine doing double click or java -jar file.jar, but if I try to open it with another java program doing
File file = new File("path/file.jar");
Desktop.getDesktop().open(file)
it´s not loading the pictures.
What can it be?
When you run java -jar file.jar, the jar file is in the current directory, and presumably so is the picture folder.
When you double-click a jar file, the code is run with the containing folder as the current directory.
When you "open" path/file.jar, the current directory is obviously different, because why else would you need to qualify the jar file name. Since you program replies on the current directory to find the picture folder, it fails.
Solutions, in my recommendation order (given the little I know of your code):
Include the pictures inside the jar file, then access them using getResourceAsStream.
Include the pictures in another jar file, have the manifest file of you main jar file include the picture jar file in the classpath.
Make sure the current directory is correct before try to "open" the file.

Make jar file from folder

i have a folder on my desktop named theme.I am new to Linux totally.
So what do I do to create a jar file from this folder?
I found this example but It doesn't work for me.
jar -cvf theme.jar
You are failing to specify the input files for the jar.
Navigate to the theme/ directory using the cd command in Terminal. If you want to include everything in this directory in your jar file, use the following command.
jar cvf theme *
Note that the * character (wildcard) indicates you want to include all files and subdirectories.
See: http://docs.oracle.com/javase/tutorial/deployment/jar/build.html
Jar files are really just zip files, renamed to .jar. Just use any folder zipping tool (such as the zip command).
zip -r theme.jar /path/to/desktop/theme

Is there a possible way to copy a file to a jar file?

I would like to ask a question if there is a way to copy a file(i.e. an image) to a .jar file, because i want to deploy a program and i created folders along with the source codes(they are in the same location as the source codes) and the folders are inside the jar file (i used Netbeans to create the jar file)..... Now what i want is i would like to copy files choosen by a JFileChooser to the folders inside the jar file????
Any idea is heartily accepted..!!! Thanks in advance????
P.S.:
I already tried searching the net for answers but all they know is how to copy the file inside the jar file to another directory......
Suppose that you want to add the file images/image1.gif to the JAR file.
jar uf <fileName>.jar images/image1.gif
To change the directory use -c
jar uf <fileName>.jar -C
in this command
jar uf jar-file input-file(s)
In this command:
The u option indicates that you want to update an existing JAR file.
The f option indicates that the JAR file to update is specified on the command line.
jar-file is the existing JAR file that's to be updated.
input-file(s) is a space-delimited list of one or more files that you want to add to the Jar file.
Related Docs
A JAR file is a ZIP compressed file.
See this S.O. answer for a solutiion to add files to an exisiting ZIP archive: Appending files to a zip file with Java

FileNotFoundException while running as Jar - Java

I have written my java codes using eclipse and iam reading a file and processing the codes. The key.txt is present in the same folder as src .
BufferedReader br= new BufferedReader(new FileReader("KEY.txt"));
Now after i compile the jar and place both the jar and file in the same folder and execute iam getting FilenotFoundException. The program works if i run inside Eclipse reading the file perfectly.
The jar file and the key.txt always have to be in the same folder.
Please let me know how can I solve this issue of FilenotFoundException.
The code you have opens a file in the current working directory, which is the directory where you started the Java process from.
This is completely unrelated to where the jar file is located.
How do you execute the jar file?
If the key file is right next to the jar file, this should work:
java -jar theJar.jar
But this will not (because the path to the key is now "test/KEY.txt" ):
java -jar test/theJar.jar
When you run a program in Eclipse, the current working directory is (usually) the project root folder.
An alternative to consider (if the file does not need to be edited by the user) is to put the key file into the jar file. Then it will not get lost, and you can load it via the classloader.
Another good option is to have the user provide the path to the file, via command line parameter or system property.
Or make a batch file / shell script that makes sure that you are always running from the proper directory.
I think you should put these files with class file of the running class, because the current directory is the directory in which the class file is stored, not the source java file.

Categories

Resources