Packing jar file into exists jar file - java

I'm writing a MP3 player in Java.
If i will finish I want to pack all .class files in one .jar file. I don't want have player, which starts by console.
If i open this one .jar file i want see player window.
I know how to pack it. I must use jar.exe packer with params: cvfm Player.jar MANIFEST.MF ./config/*.class and create MANIEST.MF which content class that has main method.
But the problem is when I want use another Look and Feel, or use existing .jar file. I can put this existing .jar file into my player main dir and compile javac.exe with parameter -cp .;./JarFile.jar, but when I pack all compiled .class files and my existsing JTattooDebug.jar file into one Player.jar file i don't see new Look and Feel i just see default view.

If you are using eclipse.
Right-Click your project
Export -> Runnable Jar File
Then select the destination for your jar, and make sure you have "Extract required libraries into generated JAR" selected. That will give you a runnable jar, complete with auto generated manifest, with all of your needed jars inside.

Solved! I had to add line:
Class-Path: lib/JTattooDebug.jar
in my MANIFEST.MF file, now all works.

Related

How do you export multiple java files as a singular runnable jar file in Eclipse?

I have a project with 3 java files:
Title_Screen .java
Game_Screen .java
Game_Code .java
Game_Screen and Game_Code use OpenGL, and Title_Screen opens Game_Code which opens Game_Screen. In eclipse, the program works perfectly, but when I try to export it as a runnable jar file, no matter what I do, it always just exports Title_Screen.java. What am I doing wrong, and what steps do I have to take to export all three java files in one .jar file?
Edit: It seems to only happen to my program, perhaps it's something to do with the OpenGL libraries?
Edit 2: I removed the libraries from my program, same results as exporting it to a jar file. My actual problem is that I can't put in the libraries.
Edit 3: Problem resolved! All I had to do was use jarsplice to create my runnable jar, not Eclipse. Tutorial I used: https://www.youtube.com/watch?v=YqGUk84BmlQ
You can use the following command to build the jar and specify the main class entry point (Main).
jar cfe output.jar Main src/Repository/* src/util/*.class
you can write multiple files when creating the jar
jar cf Output.jar src/util/Main.class src/util/SubMain.class src/Repository/*
Or from eclipse
Put all your files in a folder in your Eclipse project and then:
1.Right click in your folder
2.Export
3.Java -> Runnable JAR File
What steps are you following to export it as runnable jar?
Are you able to run the jar file successfully?
Steps to export Runnable jar is :
Right click on project and select "Export" option.
Provide the destination path where the jar file needs to be store.
Export

Java jar file not working when move to another location

Ok so... here are my steps.
I have a folder on desktop. Lets called it DesktopFolder. Inside desktop folder, i have 2 folders. One called libs and one called src. Inside libs are two jar dependencies. Lets call them jar1 and jar2. Inside src, i have a java file. lets call it MyProgram.java
so I compile them in cmd with
javac -cp .;../libs/jar1;../libs/jar2; MyProgram.java
that compiles.
now I create a manifest.txt inside my src folder with the following:
Main-Class: MyProgram
Class-Path: ../libs/jar1 ../libs/jar2
<a new line>
then in my cmd, I navigate to my src directory and do:
jar -cvfm MyProgramJar.jar manifest.txt ./../libs MyProgram.class
this compiles a jar file called MyProgramJar
this is found inside my src (because i navigated my cmd directory to src)
When I run the executable, it works.
But when i move the MyProgramJar outside to desktop
it says it cannot find library. Why is that? How can I fix it?
The error itself is the JNI error.
hmmmm. thanks to #MadProgrammer (from the comment section). I learned that a jar file cannot access another jar file from within. The classpath you put into manifest is all relative classpath. Once you move the jar file outside, it wont be able to locate the file that your program depends on. There are ways around it like "fat" jars as mentioned by Madprogrammer that allows a jar to access another jar from within.
Another way is opening up dependencies jar and simply moving the files out of its own jar. That way, your code can reference those libraries directly.
A personal friend of mine used eclipse to build the jar. That seem to work even if you have a jar within a jar and you move the jar around. I'm assuming eclipse did something just like "fat" jar.

How to Compile .java files into .jar file? [duplicate]

This question already has answers here:
How do I make a JAR from a .java file?
(8 answers)
Closed 6 years ago.
I just have some (134) .java source files with me and I'm pretty sure that contains all the necessary code!
I want some quick tutorial to compile this program into a .jar file.
(I'm on a windows platform btw)
I have tried javac and jar commands. I got my .jar file but it's not opening!!!
Thanks in advance!
Best practice is to make use of Ant/Maven/Gradle kind of build program tools that can take care of creating Jar files.
Other way is to just make use of Eclipse's feature for exporting the project as a light weight Jar file or Runnable Jar file(which includes dependencies).
Place all the files you want to include in the JAR file inside a
single folder.
Open the command prompt in Admin Mode
Navigate to the folder where you stored your files.
Set the path to the directory of the JDK bin. You will need to run
the jar.exe utility to create a JAR file, and that file is located
in the bin directory.
Create the JAR file.
The format of the command line for creating the
JAR file looks like this: jar cf 'jar-file'.jar input-file(s)
You can use WINRAR.
right click on file (put inside all your .java files) and compile by using winrar;
choose format .zip (important)
and save filename.jar (important)

How to recompile jar file from command line

I have a jar file that consists of class files and java source files together (actually android unity plugin).
I want change the behaviour of one of the function by modifying the java source code and repackage it to jar file. Is it feasible to do with command line?
Use jar xf <JAR-file> to extract the entire JAR file to whatever directory you're currently on.
Add your new code to the files, removing the old code (make sure you have copies or back everything up, just in case).
Use jar cvf <JAR-file-name> * to create a JAR using all contents in the directory of your files.

how to update a params in a jar file?

I need to update a config.params in within a jar file that was compiled by ant. I know I don't need to re-compile the source java code for doing this. Can someone help me with how to update the params file in jar file?
I need to change an integer value n the config.params file:
fileSize = 4 should be changed to fileSize = 20
Try extracting jar in a zip program like 7-Zip (or rename to .zip and open with Windows), change the file data, then re-zip (and rename back to jar if you need to).
Technically the "compiling" was done by javac (not Ant) to build .class files, which you don't need to touch since the developers were smart enough to put the setting you need into a properties file.
I did the following:
jar xf jar-file [archived-file(s)]
modified the file I wanted
jar cf jar-file input-file(s)
learned from http://docs.oracle.com/javase/tutorial/deployment/jar/build.html
an even easier way is this: jar uf jar-file input-file(s)
http://docs.oracle.com/javase/tutorial/deployment/jar/update.html (no need to touch the jar file at all)

Categories

Resources