Remove file from *.jar, using script - java

I have next package structure
webService.ear -- dependency.jar -- some.class
Can I remove some.class from .ear using cmd script and bash script?
Solution:
I wrote cmd script for this:
call unzip target\webService.ear -d tmp
call zip -d target\tmp\dependency.jar \com\mypackage1\MyClass1.class
call zip -d target\tmp\dependency.jar \com\mypackage2\MyClass2.class
call jar cvfM target\webService.ear -C tmp .
May be, this will be helpful for someone

There is no option to the jar command to remove entries.
However, the jar format is processable with zip tools, so you should be able to use a command-line zip (GNU zip, for instance) to remove members.

Related

Making manifest file in java by command prompt

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)

Java jar -C option

I've start to read about jars, But I cannot understard how jar -C works.
I've tried the following command
jar cfv Some.jar -C innertest\ForDelete.class .
when I am in E:\eclipse_workspace\Test\bin\test>.
So I expect to have PartJar.jar and files the files in it which are not in test directory. But after executing the command the result is added manifest and only manifest file is created.
So if I try jar cfv Some.jar -C innertest . it will work, but what if I want to add only 3 files in this directory not the whole directory.
Thanks in advance.
I'm not sure whether there is a solution which does not involve multiple use of -C:
jar cfv my.jar -C myDir file1 -C myDir file2
A possible workaround, if applicable to your case, could be to put only the files you need inside a new directory and use:
jar cfv my.jar -C newDir .

Shell script with jar file at the end

I download an archive file. In the archive there will be a file that has a .sh. extension. When I opened that file with VI I found the below code in the beginning of the file:
#!/bin/sh
MYSELF=`which "$0" 2>/dev/null`
[ $? -gt 0 -a -f "$0" ] && MYSELF="./$0"
java=java
if test -n "$JAVA_HOME"; then
java="$JAVA_HOME/bin/java"
fi
exec "$java" $java_args -jar $MYSELF "$#"
exit 1
I can run the jar by doing java -jar file or `./file'.
Can someone explain me what is going on? How can you create such file?
Try by yourself the following commands. Start creating a normal jar file with any content, or use someone you have. I will name it "myjar.jar"
Next, create a file "hello.sh" with content:
#!/bin/bash
exec echo hello
now, add this file at start of a new jar file:
cat hello.sh myjar.jar > mytrick.jar
chmod 700 mytrick.jar
And finally, the interesting part, type:
./mytrick.jar
jar -tf mytrick.jar
unzip mytrick.jar
in other words, usually jar/unzip skips any content until their own header. Moreover, a shell script "ends" in a line who call "exec" (because shell interpreter is replace at this point by the command in the exec line).
However, this trick is based in a behaviour of jar/unzip probably out of standards. Note, by example, that this statement fails (has no effects):
jar -xf mytrick.jar
If the file after extracting the tar file is start-superbeam.sh try doing chmod +x start-superbeam.sh && ./start-superbeam.sh or /bin/sh ./start-superbeam.sh.
If the program has arguments, supply them after at the end. It will run java on that the superbeam.sh which as a jar file at the end.
If you need special java parameters set such as for memory size, you would set them in the environment variable java_args.
As for what's going on this is a shell script with a jar file at the end of it after the exit. To quote from ReallyExecutable Jars:
There has long been a hack known in some circles, but not widely
known, to make jars really executable, in the chmod +x sense. The hack
takes advantage of the fact that jar files are zip files, and zip
files allow arbitrary cruft to be prepended to the zip file itself
(this is how self-extracting zip files work).
As for how to create, see the accepted answer or the link.

jar - cvf command is not running?

I created a wordcount example in MapReduce and trying to make the jar file using following command:
hduseros#MT2012018:/usr/local/hadoop$ jar -cvf playground/wordcount.jar -C playground/classes
But Its not at all running and giving me following result:
hduseros#MT2012018:/usr/local/hadoop$ jar -cvf playground/wordcount.jar -C playground/classes
Usage: jar {ctxui}[vfm0Me] [jar-file] [manifest-file] [entry-point] [-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
-e specify application entry point for stand-alone application
bundled into an executable jar 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, the archive file name and the entry point name are
specified in the same order as the 'm', 'f' and 'e' flags.
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/ .
Please correct where I am wrong. I have set the class path. I can run the examples already present but not able to make jar file.
That is probably because you haven't given it the location where it needs to pick the files from. Try this:
jar -cvf playground/wordcount.jar -C playground/classes .
And include the last dot (.)
Try eliminating the dash: jar cvf, not jar -cvf
Try to perform the following command:
jar cvf playground/wordcount.jar playground/classes/*.class
See also: Building a JAR File of Interface Classes
-C option just changes your directory to the argument you have given afterwards. You still should add the files/directories to be added to the jar file after you have switched to that directory.
Please check the documentation for jar command.

creating jar using -C flag

I am trying to create a Jar file from command line using -C flag, but every time it returns a help screen.
I am giving following command.
user#ubuntu:~/CDH/JAVA_WORKSPACE/JAVA-SETUP$ jar cvfm ./build/jar/Setup.jar MANIFEST -C build/classes/com/demo/Setup.class
If I remove -C command then it archives fine.
But if -C flag is there, then it always returns jar help page.
Am I doing something wrong here?
Your command line option is:
-C build/classes/com/demo/Setup.class
The jar tools wants the directory name to follow the -C and then the file. You need two words to follow "-C" like this:
-C build/classes/com/demo Setup.class

Categories

Resources