I've edited this question, and now it is exactly what I did:
I want to create a .jar file from some .class files in the command line.
I worked on Eclipse, and created:
myProject project, and in it: myPackage package, and in it: myClass class.
Then I wrote in the command line:
jar -cfv myJar.jar myPackage\myClass.class
And I got this:
added manifest
adding: myPackage/myClass.class(in = 745) (out= 473)(deflated 36%)
This really created the myJar.jar file in my current directory. Now, I wanted to check if the process was done successfully, so I extracted the class from the jar thus:
jar xfv myJar.jar
And I got this:
created: META-INF/
inflated: META-INF/MANIFEST.MF
inflated: myPackage/myClass.class
And this created for me just the META-INF folder, with the MANIFEST.MF in it, but I don't see any .class file here!!
It seems like something in the packing to jar process is incorrect.
Anybody has an idea??
Any answer is appreciated!
According to the output you gave, there is no myClass.class file in the directory where you execute
jar -cf myJar.jar myClass.class
So obviously, the command can't add it to the jar: it doesn't exist. If you want to add the myClass directory, recursively, to the jar file, then use
jar -cf myJar.jar myClass
EDIT:
Just look at the output:
inflated: myPackage/myClass.class
The myClass.class file is there in the jar file. There is no problem at all.
Related
I have a class file Main.class which needs a JAR file abc.jar to run.
Both files are in the same directory. Now I try to run the class file with
java -cp "." Main
but I get a java.lang/NoClassDefFoundError.
I thought -cp "." tells the classpath to include the current directory, but somehow it doesn't.
How do I get this JAR file in the current directory on the class path?
Thanks to patrinox' comment I figured it out:
The JAR itself needs to be in the CLASSPATH property, not only the directory containing the JAR. Therefore the command line has to read:
java -cp ".:./abc.jar" Main
I have below project structure:
->bin
->lib
->resources
->src
->DemoFramework
->FirstDemo.java
In lib folder, I have an external jar that I need in my application. Its name is ext.jar. In resources, I have Manifest.txt file, whose content is given below.
Manifest-Version: 1.0
Class-Path: . lib/ext.jar
Main-Class: DemoFramework.FirstDemo
I am using below command to generate jar:
javac -cp ".;./lib/ext.jar" src/DemoFramework/*.java -d bin
Basically I am putting all class files into bin folder so that in final jar file, source code is not visible.
Then I am issuing below command:
jar cmf resources/Manifest.txt project.jar bin lib
The jar file is successfully created but when I run it, it says:
no main manifest attribute, in project.jar
I am confused about this error, no idea why it is happening.
Can you guys help me to sort it out?
Thanks.
I'm trying to create a jar file and run it using java -cp main.jar com.test.Foo.Main but I keep getting:
Error: Could not find or load main class com.test.Foo.Main
Caused by: java.lang.ClassNotFoundException: com.test.Foo.Main
This is my file structure. So I'm thinking the line in my Main.java should be package com.test.Foo correct?
I'm compiling my Main.java with javac Main.java which outputs a Main.class file. Afterward, I create a jar file using jar cfm main.jar META-INF/MANIFEST.MF Main.class and finally while I'm in the same directory as the jar file <root>/src/com/test/Foo/ I run java -cp main.jar com.test.Foo.Main and that's when I run into the above error. Any idea how I can run this file like this (and yes I need it to run with this command specifically)?
Main.java
package com.test.Foo;
public class Main {
public static void main (String args[]) {
System.out.println("I am com.test.Foo.Main");
}
}
META-INF/MANIFEST.MF
Manifest-Version: 1.0
Main-Class: com.test.Foo.Main
I tried using some of the options given in this popular SO question and nothing helped.
The picture you're showing in your question is your project structure not your jar structure.
When you create a jar file, the structure for that jar file might be
different with your source code folder structure.
Every IDE (such as eclipse, netbeans, IntelliJ) has a mechanism for creating JAR files. In your case when you open the created jar file (using zip apps like winrar) you should see something like this :
com
|
test
|
Foo
|
Main
META-INF
|
MANIFEST.MF
This should be the ordering of your files and folders, otherwise Java can not find your main class from MANIFEST.MF
Now to solve this problem:
Open your jar file using a zip application like winrar
check the folder structure residing inside your jar file as I draw
Fix it right away within the winrar or try to correct your project structure to produce the structure I mentioned.
The class is called com.test.Foo.Main you need to specify the full name in the command:
java -cp main.jar com.test.Foo.Main
or you can use the simpler
java -jar main.jar
Check your META-INF/MANIFEST.MF for the attribute of Manifest-Version: 1.0
This attribute must be there.
Edit:
You need to move to the source root src/ and issue below command to create a valid jar.
javac com/test/Foo/*.java
and, create the jar using,
jar cmf com/test/Foo/MANIFEST.MF main.jar com/test/Foo/*.class
The thing is, package structure should match with the folder structure apparently.
I have a jar file in C:\ (xx.jar). I need to replace a class file (path: com\sample\folder\xfile.class) within xx.jar with a class file (yfile.class) that is placed C:\.
Please help me with the command for replacing one class file in the jar with another class file outside the jar. I am struggling in pointing the directory structure.
I tried with,
jar uf C:\xx.jar C:\yfile.class
The above command creates a new folder C:\ within xx.jar and the yfile.class comes inside C:\. But I want the file to be inside the com.sample.folder in the xx.jar
Kindly help. Thanks in advance!
You can't, really. Jar is really meant to package a prepared folder.
I guess this is also why maven etc create a build folder with everything that is going to end up in the jar/war/ear file.
jar uf C:\xx.jar C:\yfile.class
is indeed just adding the file to the archive.
In the folder you are archiving from you can use some -C directive, but that is about it. Suppose yout myfile.class is in a dir called C:\classes and you cal jar from that folder
jar uf C:\xx.jar -C .. classes\file.class
Is going to add the folder classes to the jar with the file myclass.class in it.
Kind regards
I've test simple scala program and compiled it with scalac
object Test {
def main(args: Array[String]) {
println("hongseok yoon")
}
}
If I run that 'java -cp .;scala-library.jar Test' and it works okay.
YES, scala-library.jar filee is in same directory.
This is my manifest file manifest.txt
Class-Path: scala-library.jar
Main-Class: Test
and I make jar with 'jar cfm Test.jar manifest.txt *.class'
If I unzip generated Test.jar file and open manifest file, it does not contain Main-Class field. So, It cannot be run with 'java -jar Test.jar'
I can edit it manually and Test.jar runs well.
Why does jar miss Main-Class field? and how to fix this?
(If I switch order of fields, only first field is shown)
ONE more empty line is needed at the end of manifest file.
What a stupid restriction! :(