I'm not very skilled in writing batch files and/ or java. I have a folder with several .class-Files and folders in it and I want to put them all into a executable .jar-File. I know that there is a tool called "jar - The Java Archive Tool", but that just won't work for me. My folder structure looks like this:
Folder "test"
File (a.class)
Folder "subdirectory"
File (b.class)
I want a executable .jar-File called file.jar. In this .jar should now be the file a.class and the folder subdirectory with the file b.class in it.
I don't get the .jar-Tool to run and the 7zip command line doesn't support .jars (I can't even add files to it). I want this to run from a .bat-File, so I just have to open the batch-file, it creates the .jar and puts the files in it and then closes itself again.
I hope you can help me and get what I mean.
If you have issues in executing jar command, then probably you would need to check if your path has been set correctly. You can verify if the path contains jdk location ( echo %path%) from command prompt. If not you need to update. Also you can verify using Javac -verbose command to see jdk is installed.
Once you have jdk path set, you can try below command to create jar
Jar -cf myapp.jar * --> includes all files including files from sub folders.
If you want to run from batch, you would need to mention path before jar command. Ideal place for path is to configure as environment property.
Create a text file (and call it something useful like manifest.txt). In it, put the line:
Main-Class: a
(which should be called A by convention) and include a hard return at the end.
Then create the jar with:
jar cvfm file.jar manifest.txt *.class
or
jar cvfm c:\somedir\file.jar c:\somedir\mainfest.txt *
You could just put that line in a batch file called createJar.bat and run it if you only want to create files called 'file.jar'
hth
Related
Lets say that I built a GUI Application using NetBeans. To run this java application I need to open source code in IDE and then run. I know that I can also run through command prompt.
But how do I start the application independent of IDE. Isn't there some .exe file or something like that, which on double clicking directly runs the application?
If not, how do I generate such a file?
Here you can find how to create .jar in Netbeans: How to create a Jar file in Netbeans
You can run the executable jar on every single computer, on one condition - the system have JRE installed.
If you want to, you can also build the .jar using command line, to do that use the following command:
jar cf jar-file input-file(s)
Description from Oracle doc:
The options and arguments used in this command are:
The c option indicates that you want to create a JAR file. The f
option indicates that you want the output to go to a file rather than
to stdout. jar-file is the name that you want the resulting JAR file
to have. You can use any filename for a JAR file. By convention, JAR
filenames are given a .jar extension, though this is not required. The
input-file(s) argument is a space-separated list of one or more files
that you want to include in your JAR file. The input-file(s) argument
can contain the wildcard * symbol. If any of the "input-files" are
directories, the contents of those directories are added to the JAR
archive recursively. The c and f options can appear in either order,
but there must not be any space between them.
This command will generate a compressed JAR file and place it in the
current directory. The command will also generate a default manifest
file for the JAR archive.
After you build your application look for a folder named "dist" in your project's folder. You should find there a file *.jar which can be run anywhere with double click.
STEPS TO FOLLOW:
create a jar
run the jar
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
I have a jar file and a class file. I want to create a single JAR file from both. Is it possible? Please help me on the same.
I have tried
Jar -cf new.jar sample1.class sample2.jar
It didnot help
Not sure what you are trying to do, but technically you can do it with:
cp sample2.jar new.jar
jar uvf new.jar sample1.class
Of course, if you already had class sample1.class inside sample2.jar, it will be overwritten ( "u" == update ) by sample1.class
Transfer all your classes into a folder, say it is in C:\Folder\Project\
Go to the same folder from command prompt. And execute the following code :
jar cfv Project.jar *
In the above command, c will create jar, f will transfer the content into the file i.e Project.jar and v will print the process done into the console.
* (WildCard) will take all the files present in the folder and the corresponding jar would be created in the same folder.
You can find more information the DOCS.
http://docs.oracle.com/javase/tutorial/deployment/jar/basicsindex.html
In the meanwhile i would suggest you to go through the below link, where you can do the same with a build tool like Ant or Maven.
Clean way to combine multiple jars? Preferably using Ant
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
How do I convert:
class file to jar file using cmd?
class file to exe file?
jar file to exe?
Can I convert exe file to jar file?
Class files to jar files
Usage: jar {ctxu}[vfm0M][jar-file]
[manifest-file] [-C dir] files ...
Options:
-c create new archive
-t list table of contents for archive
-x extract named (or all) files from archive
-u update existing archive
-v generate verbose output on standard output
-f specify archive file name
-m include manifest information from specified manifest file
-0 store only; use no ZIP compression
-M do not create a manifest file for the entries
-i generate index information for the specified jar files
-C change to the specified directory and include the following
file If any file is a directory then it is processed recursively. The manifest file name and the archive file name needs to be specified in the same order the 'm' and 'f' flags are
specified.
Example 1: to archive two class files into an archive called classes.jar:
`jar cvf classes.jar Foo.class Bar.class`
Example 2: use an existing manifest file 'mymanifest' and archive
all the files in the foo/ directory into 'classes.jar':
jar cvfm classes.jar mymanifest -C foo/ .
Convert jar files to .exe file
1)JSmooth .exe wrapper
2)JarToExe 1.8
3)Executor
4)Advanced Installer
Convert .class to .exe is discussed in length here
You must have missed this. Please look into Java Archive (JAR) Files Guide.
And surely missed Real's How to for it Make a JAR executable There are multiple wrappers that do this work (converting Jar to exe, platform specific). You just need to search in StackOverflow for Jar exe
To convert (actually, package) .class files into .jar files, you use the jar tool. You can then generate a .exe file from the .jar using tools like Launch4j, JSmooth or several other packages (search the web for "jar exe").
If you are using Netbeans IDE, then creating .exe file from .class file won't take much time. In IDE, create a new project and put your java program in this project. Now follow these steps-
Right click the project and choose properties.
Choose run from left panel and enter the main class(in which main method is defined) in right panel.
Again right click the project and choose add library. Select swing layout extensions. Click add library.
4.Now select clean and build from run menu in your IDE. Make sure that you have already set your this project as main project.
Open the CMD and set the directory to your project. Go to "dist" > and type java -jar jarname.jar.
Your program is running in cmd now.
Open launch4j and provide the information required.
Run and enjoy your application.
Create jar file using command line. Answer in http://download.oracle.com/javase/tutorial/deployment/jar/build.html
Class to exe. Answer in http://www.javacoffeebreak.com/faq/faq0042.html
Jar to exe. Answer in http://viralpatel.net/blogs/2009/02/convert-jar-to-exe-executable-jar-file-to-exe-converting.html
Converting exe to a jar file is possible. But involves reverse engineering to discover what is in the exe, extract and do whatever you want with it. In my opinion, is not worth at all.