I have a program in java which uses a lot of classes and now I wish to make a single .jar file for all of them. How do I do this using cygwin in windows 8.
My adviser told me to use cygwin and type make in my devel and jars directory, however after doing this I am unable to figure where the jar file is create.
Thanks for your time and responses.
You can do it like that :
Create a manifest file. for ex, i've created a MANIFEST.MF in the same folder :
Main-Class: HelloWorld
(don't forget the carriage return at the end)
Compile your java code :
javac.exe *.java
Create the executable JAR file :
jar.exe cvmf MANIFEST.MF hello_word.jar *.class
Execute the .jar :
java.exe -jar hello_word.jar
You can specify the path when you create the jar file :
jar.exe cvmf MANIFEST.MF /path/you/want/hello_word.jar *.class
Related
I want to know how to export a jar file from within java code. Look at the following situation:
I have some .java files including one main class. I want to know how to pack those files and convert them to an executable jar file. I don't want to do this the traditional way by exporting the project to a jar file using eclipse built in functionality (I know how to do that). I want to know how to export the .java files and the required libraries to a jar file using java code.
Here is my ideal result: I run my own jar exporting program and it asks me to select some files. When I do select and hit 'export', it packs those files in an executable jar file. Thanks in advance and peace out.
Maybe you could try running cmd commands for creating JAR file using JAVA code.
You can create jar File in Command Prompt following these steps:
- Start Command Prompt.
- Navigate to the folder that holds your class files:
C:\>cd \mywork
- Set path to include JDK’s bin. For example:
C:\mywork> path c:\Program Files\Java\jdk1.7.0_25\bin;%path%
- Compile your class(es):
C:\mywork> javac *.java
- Create a manifest file
manifest.txt with, for example, this content:
Manifest-Version: 1.0
Created-By: 1.8.0_171 (Oracle Corporation) #your jdk version
Main-Class: mainPackage.MainClass
- Create the jar file:
C:\mywork> jar cvfm Craps.jar manifest.txt *.class
To see how can we run these commands within JAVA code, follow this link:
Run cmd commands through Java
I have an executable jar and i want to know the name of java main class from this jar
My question are if there is a java cmd to get this from jar metadata ?
You can use unix command :
unzip -p XXX.jar META-INF/MANIFEST.MF
or you can use jar commad
jar tf XXX.jar and see the content of MANIFEST.MF.
Technically a jar file can contain more than one main class.
When java executes a jar file, it looks in the META-INF/MANIFEST.MF file inside the jar to find the entrypoint.
There is no direct command to get this information, but you can unpack the jar (it's just a zip file) and look into the manifest yourself.
More about the manifest: https://docs.oracle.com/javase/tutorial/deployment/jar/defman.html
More about application entrypoints: https://docs.oracle.com/javase/tutorial/deployment/jar/appman.html
I have program in Java in path: C:\...\MyProgram.
This program have some dependencies to others *.jar files. I would run it using cmd. So what I do:
in cmd I write cd C:\...\MyProgram\bin and then java -cp C:\...\MyProgram\*;. main.Main. It is working. But now I exported MyProgram to jar file. Could you tell me how can I run this now? So I have file MyProgram.jar with these same dependencies. How run it by using cmd?
Folders and archive files
When classes are stored in a directory (folder), like /java/MyClasses/utility/myapp, then the class path entry points to the directory that contains the first element of the package name. (in this case, /java/MyClasses, since the package name is utility.myapp.)
But when classes are stored in an archive file (a .zip or .jar file) the class path entry is the path to and including the .zip or .jar file. For example, to use a class library that is in a .jar file, the command would look something like this:
% java -classpath /java/MyClasses/myclasses.jar utility.myapp.Cool
Found in http://docs.oracle.com/javase/7/docs/technotes/tools/solaris/classpath.html
You need to add a Class-Path entry to the manifest file (META-INF/manifest.mf) inside the jar:
Class-Path: /C:/.../MyProgram/ .
This assumes that there are dependent classes under C:/.../MyProgram/, not jar files.
You should also add an entry for the Main-class:
Main-Class: main.Main
Then just execute your jar as
java -jar MyProgram.jar
Set the path of JAR file in your classpath and then execute the other JAR file.
To add JAR using eclipse.
Right click on project -> properties
Java Build Path -> Click add external JARs.
This will add JAR to your classpath.
I have some java code written in a text file, now I want to create a jar file to use that file as a library in my android project. So what would be the best way to make a jar file from this point. Please note that I have some code written in a textfile and saved as a .txt. If I rename that file to .java and use this code
jar cf filename.jar file(s)
a jar file is created but when I decompile it in java decompiler it doesnt show the packages and that codes that's why I am unable to use it's methods. What would be the best way to do this? I need help
If you want to use the classes in your library they must be compiled and you have to manually create all the packages subfolders.
So :
1. Make a folder
2. Create each package subfolderds in it (com/foo/bar/xxx)
3. Compile each of your .java files and put them it the correct subfolders
4. Zip your folder and rename it with a .jar extension
But it is hard to do this by code, why don't create it with your IDE ?
Creating a jar File in Windows Command Prompt
Start Command Prompt.
Navigate to the folder that holds your class files:
cd \mywork
Rem -- Set path to include JDK’s bin. For example:
path c:\Program Files\Java\jdk1.5.0_09\bin;%path%
Rem -- Compile your class(es):
javac *.java
Rem -- Create a manifest file:
echo Main-Class: DanceStudio >manifest.txt
Rem -- Create a jar file:
jar cvfm DanceStudio.jar manifest.txt *.class
Rem -- Test your jar file by trying to execute it
Rem -- This will not apply for a library JAR file without any main
java -jar DanceStudio.jar
I've made a jar file for all my classes and yes it works. Although it's not running my custom GUI. I'd like to set the jar file to run my GUI before anything else, and from there the rest of the files will be executed.
How would I do this? Thanks in advance.
When I run it through the command prompt using
java GUI
it works, but I need to do this using a jar file as I want my class files obfuscated.
I presume you jave just .class files now and you want to create a .jar file.
To run a jar file you must add -jar after the java command.
Jar files are zip files which contain the classes, possibly resources and a manifest that tells which class has the main() method.
The jar command is used to create a jar file.
jar cvfm output.jar Manifest.txt *class package1 package2
and in Manifest.txt you should have this line:
Main-Class: MainClass
After that just run:
java -jar output.jar