I have a folder structures
/com/cdy/ws/a.class files
/com/cdy/ws/b.class files
/com/cdy/ws/c.class files
When I run the following command “jar cvf asd.jar *.class” it gives jar with all the class files.
But the folder structure is not getting generated. All the class files have to be under “com.cdy/ws” but all the classes are in same level of META-INF.
Can anyone tell me what is the command to generate the package structure?
Thanks
You need to start creating the JAR at the root of the files.
So, for instance:
jar cvf program.jar -C path/to/classes .
That assumes that path/to/classes contains the com directory.
FYI, these days it is relatively uncommon for most people to use the jar command directly, as they will use a build tool such as Ant or Maven to take care of that (and other aspects of the build). It is well worth the effort of allowing one of those tools to take care of all aspects of your build, and it's even easier with a good IDE to help write the build.xml (Ant) or pom.xml (Maven).
You want
$ jar cvf asd.jar .
to specify the directory (e.g. .) to jar from. That will maintain your folder structure within the jar file.
From the directory containing the com folder:
$ jar cvf asd.jar com
added manifest
adding: com/(in = 0) (out= 0)(stored 0%)
adding: com/cdy/(in = 0) (out= 0)(stored 0%)
adding: com/cdy/ws/(in = 0) (out= 0)(stored 0%)
adding: com/cdy/ws/a.class(in = 0) (out= 0)(stored 0%)
adding: com/cdy/ws/b.class(in = 0) (out= 0)(stored 0%)
adding: com/cdy/ws/c.class(in = 0) (out= 0)(stored 0%)
$ jar -tf asd.jar
META-INF/
META-INF/MANIFEST.MF
com/
com/cdy/
com/cdy/ws/
com/cdy/ws/a.class
com/cdy/ws/b.class
com/cdy/ws/c.class
Step 1: Go to directory where the classes are kept using command prompt (or Linux shell prompt)
Like for Project.
C:/workspace/MyProj/bin/classess/com/test/*.class
Go directory bin using command:
cd C:/workspace/MyProj/bin
Step 2: Use below command to generate jar file.
jar cvf helloworld.jar com\test\hello\Hello.class com\test\orld\HelloWorld.class
Using the above command the classes will be placed in a jar in a directory structure.
Your specified folderName must be on C:\Program Files\Java\jdk1.7.0_02\bin path
Foldername having class files.
C:\Program Files\Java\jdk1.7.0_02\bin>jar cvf program1.jar Foldername
Now program1.jar will create in C:\Program Files\Java\jdk1.7.0_02\bin path
this bellow code gave me correct response
jar cvf MyJar.jar *.properties lib/*.jar -C bin .
it added the (log4j) properties file, it added the jar files in lib. and then it went inside bin to retrieve the class files with package.
Simply more than above -
Keep your Java packaging contents inside a directory and make sure there is nothing inside except your Java packages and their corresponding Java classes.
Open Command(If Windows) Prompt, reach to the containing directory path like below -
C:> cd "C:\Users\UserABC\Downloads\Current Folder"
C:\Users\UserABC\Downloads\Current Folder>jar cvf hello-world-1.0.1.jar .
To avoid to add sources files .java to your package you should do
cd src/
jar cvf mylib.jar com/**/*.class
Supposed that your project structure was like
myproject/
src/
com/
mycompany/
mainClass.java
mainClass.class
This is what I do inside .sh file, let's consider install.sh
#!/bin/sh
echo Installing
cd .../Your_Project_Directory/com/cdy/ws/
jar cfe X.jar Main *.class
cd .../Your_Project_Directory/
ln -s .../Your_Project_Directory/com/cdy/ws/X.jar X
echo Testing...
java -jar X
echo You are Good to Go...Use hapily
#etc.
Creating Executable Jar file at the Class directory and creating a SymLink at anywhere you want.
Run it using,
$ sh install.sh[ENTER]
$ java -jar X[ENTER]
Assume your project folder structure as follows :
c:\test\classes\com\test\awt\Example.class
c:\test\classes\manifest.txt
You can issue following command to create a “Example.jar.
jar -cvfm Example.jar manifest.txt com/test/awt/*.class
For Example :
go to folder structure from commmand prompt "cd C:\test\classes"
C:\test\classes>jar -cvfm Example.jar manifest.txt com/test/awt/*.class
Go the root folder of the package, in our case it’s com.
Press shift and right-click, then open cmd.
Run this command: jar cvf test.jar .
The dot(.) used at the end of the above command informs the jar operation to package the classes starting from the current path.
After running the command, you will get a jar file called test.jar besides the com folder.
ref:
https://initialcommit.com/blog/how-to-create-a-jar-file-with-a-package-structure
I've create simple java project with single class - Main with main method printing Hello.
package com.foo;
public class Main {
public static void main(String[] args) {
System.out.println("Hello!");
}
}
Code was compiled to bin directory. I'm trying to create jar using command
jar -cfe project.jar com.foo.Main -C bin\
with no results, always returning Error parsing file arguments error.
I also tried many different variations, like
jar --create --file project.jar --main-class com.foo.Main -C bin
but none of it worked. I'm using Java 16
Try running this. It should create the 'project.jar'
jar --create --file project.jar --main-class com.foo.Main -C bin .
-C flag Temporarily changes directories (cd dir) during execution of the jar command while processing the following inputfiles argument.
This command changes to bin directory and adds to project.jar all files within the bin directory (without creating a bin directory in the jar file).
For more information check the -C part in the OPTIONS section :
https://docs.oracle.com/javase/7/docs/technotes/tools/solaris/jar.html#options
You need both a "=" after main-class and a "." at the end to selct al lthe files
jar --create --file project.jar --main-class=com.foo.Main -C bin .
or
jar cfe project.jar com.foo.Main -C bin .
I'm trying to make a 'jar file' and run it by first making manifest file.
as I enter jar command in command prompt and using all explanation, this is what I think is sensible:
>md build\classes ----first I made a directory for my class files--
>javac -d build\classes src\*.java ----then compile java files from src folder to classes folder----
>javadoc src\*.java -d doc ----make documentation----
>jar -cfm my.jar m.txt -C build\classes ----I want to make a my.jar file and put the manifest contents from .class in build \classes to m.txt but I don't know what's wrong that I can't make a manifest file as m----
java -jar my.jar
I mentioned above the problem in making manifest file so that I can run .jar file.
what's wrong with my code for making the manifest file?
this is what it print when I run the command for making manifest file:
Usage: jar {ctxui}[vfmn0Me] [jar-file] [manifest-file] [entry-point] [-C dir] files ...
Simply add a . at the end of your jar.exe command, or whatever you wish to include from the build\classes directory. The -C command allows you to change directory, but you then need to specify what to include from that directory thereafter.
So...
jar -cfm my.jar m.txt -C build\classes .
I'm assuming that your manifest file is already created. If not, then you can echo via the command line and output it to a file. For example...
echo Main-Class: path.to.YourClass > MANIFEST.MF (or m.txt)
I have multiple .java files inside a folder (e.g temp/code/project)
I want a batch file that will compile and run these java files.
The batch file should create class files inside the same structure where java files are located starting with the classes folder (i.e classes/temp/code/project)
I don't know how to write a batch file. Can any body help me with this? Thanks in advance.
Theoretically you should write the following:
#echo off
::compile classes
javac -cp YOUR_CLASSPATH com/yourcompany/YourClass1.java
javac -cp YOUR_CLASSPATH com/yourcompany/YourClass2.java
javac -cp YOUR_CLASSPATH com/yourcompany/YourClass3.java
javac -cp YOUR_CLASSPATH com/yourcompany/YourClassLauncher.java
:: create jar
jar cvfM Manifest.txt myjar.jar *.class
echo.
echo Hit any key to launch project.
pause
java -jar myjar.jar
pause
If you have one class that depends on all other classes in your project it is enough to run javac with this class only: compiler will compile everything.
# Manifest.txt
Manifest-Version: 1.0
Class-Path: .;MyUtils.jar
Created-By: 1.6.0
Main-Class: com.yourcompany.YourClassLauncher
This is only example and a good exercise. In real life people use special build tools like good old Ant, Maven or newer Graidle or Buildr. I'd recommend you to take one of them.
Something along the lines of
cd C:\temp\code\project
javac -classpath . -d C:\classes\temp\code\project\ *.java
You may not need to include the -classpath tag (I always do so that I don't have to worry about maintaining my CLASSPATH variable. The -d tag specifies a particular directory to place generated class files.
I'm puzzled by the process of running java programs, maybe you can help.
I have several .java files in ~/working_dir/org/project/ that have main functions, and I want to package them in a jar to run them. I do:
cd ~/working_dir/org/projectname
javac -classpath $CLASSPATH *.java
cd ~/working_dir/
jar cf myjar.jar org/
And then try to run one of the classes in the jar by doing:
java -cp myjar.jar org.project.SomeClass
and get
Exception in thread "main" java.lang.NoClassDefFoundError: org/project/SomeClass
Could not find the main class: org.project.SomeClass
What do I do wrong? The classes compile without any errors, and jar tf myjar.jar shows that they're indeed there. As far as I know I don't need to create a Manifest file because I provide the class from which I want to run the main function at runtime - or am I wrong here?
Help much appreciated!
If the exploded jar org/project/SomeClass is beneath your current working dir:
/ <- you are here
+---/org
|
+-----/project
|
+--------SomeClass.class
try java -cp . org.project.SomeClass instead
First of all, note that if you simply do
javac org/project/SomeClass.java
the class file will end up right beside the .java file which makes it tricky to include only .class-files in the jar. I suggest you use the -d option to specify destination directory:
javac -d bin org/project/SomeClass.java
Have a look at the following bash-session for details to get it working:
A listing of the source directory:
user#host:/working_dir/src$ ls -R
.:
org
./org:
projectname
./org/projectname:
SomeClass.java
The SomeClass.java file:
user#host:/working_dir/src$ cat org/projectname/SomeClass.java
package org.project;
public class SomeClass {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
Compile it (with target directory ../bin)
user#host:/working_dir/src$ javac org/projectname/SomeClass.java -d ../bin
List the result and make sure you got the directories right:
user#host:/working_dir/src$ cd ../bin/
user#host:/working_dir/bin$ ls -R
.:
org
./org:
project
./org/project:
SomeClass.class
Create the jar file:
user#host:/working_dir/bin$ jar cf myjar.jar org
Make sure you got the directories right and didn't accidentally include the "bin" directory:
user#host:/working_dir/bin$ jar tf myjar.jar
META-INF/
META-INF/MANIFEST.MF
org/
org/project/
org/project/SomeClass.class
Launch the main method:
user#host:/working_dir/bin$ java -cp myjar.jar org.project.SomeClass
Hello World
user#host:/working_dir/bin$