I have a directory, update, with the same structure as a JAR file Application.jar. Now I want to (programmatically) replace all files in Application.jar with the files in the update folder. Lets say I have the following structure for both directory and jar file:
Application.jar / update
- TestDir
- text2.txt
- text1.txt
When using jar uf Application.jar update/TestDir/text2.txt it creates a new directory in Application.jar - update - and it's going to be added all from there, even worse when using absolute paths (Creating the whole C:\ structure inside the jar file). How can I preserve the structure, is there some way to mark pathToUpdate\update as a root? I know there's the -C parameter, but I don't understand it.
You have two options.
First change the working directory to update so that the relative path to the 'update' file matches what you want in the jar:
cd update
jar uf ../Application.jar TestDir/text2.txt
use the -C option:
jar uf Application.jar -C update TestDir/text2.txt
It's possible the -C option requires that you do: jar Application.jar -C update update/TestDir/text2.txt.
In general, I advise the first option; the second gets tricky once you start using e.g. * and the like.
Related
Quick question (sorry struggling with time)
How can I add any file from my file system under WEB-INF/classes folder in a jar file? I am trying this with jar command but it copies a file in a jar with its absolute path meaning if I create a file at E:\folder1\WEB-INF\classes folder and try to add it to a jar it creates folder1/WEB-INF/classes folder inside the jar and places the file into it.
I used jar command as below
jar -uvf E:\folder1\sample.jar E:\folder1\WEB-INF\classes\pkfe
you need to specifiy the -C-switch and alter the file-argument like so:
jar -uvf E:\folder1\sample.jar -C E:\folder1 WEB-INF\classes\pkfe
This makes the command run within E:\folder1 and include the files relative from that location
You can also add entire folders this way:
jar -uvf -uvf E:\folder1\sample.jar -C E:\folder1 .
The . tells the command to add everything at the location of the working directory (E:\folder1). You need to move your sample.jar to somewhere else, otherwise you would softlock the command, as it would be adding itsself to itsself into eternity...
Several specific files within a folder would work like this
jar -uvf E:\folder1\sample.jar -C E:\folder1 WEB-INF\classes\pkfe -C E:\folder1 WEB-INF\classes\secondFile.dat -C E:\folder1 org\blupp\blah\thirdFile.class
Source: jar -?
build> jar cvfe test\MyJavaLibrary.jar Main -C test\java Main.class foo\SomeClass.class
foo\SomeClass.class : no such file or directory
added manifest
adding: Main.class(in = 444) (out= 308)(deflated 30%)
The corresponding directory structure is
build/
test/
java/
foo/
SomeClass.class
Main.class
It is very strange that it works correctly when using test\java\foo\SomeClass.class as the last argument instead. Fine, but that doesn't work when I do the same for the first argument, that is test\java\Main.class. The behaviour appears to be very inconsistent.
Why can it not find foo\SomeClass.class, or better, what is the right way to create the Jar?
java version "1.8.0_102"
The executable that you're running from the command-line appears to start in the build directory, and as such, it doesn't know where the foo\SomeClass.class file is relative to build - this would also explain why test\java\foo\SomeClass.class works (it knows how to find the file relative to the execution directory).
If you were to have the foo folder on the top level inside build, I believe it would work correctly, but because foo is nested within two layers of directories, the executable has no idea where to look for a folder with that name in its current directory.
I just found the answer in a documentation page I haven't seen before.
-C dir
Temporarily changes directories to dir while processing the following inputfiles argument. Multiple -C dir inputfiles sets are allowed.
So apparently the -C argument would need to be placed for every input, not just specified once.
Source: http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jar.html
I have an app that runs on the JVM, provided as a fat jar. It needs to invoke a native Linux binary, for example ffmpeg. What directory structure would a Zip file need to contain in order to package both the jar and the executable together? I cannot find documentation, just code examples using build tools that I have not worked with.
Let's pretend the name of my lambda is blah. I am hoping to get an answer like:
Deployable jar contains:
+ blah/ # contains fat jar
+ lib/ # contains ffmpeg
Here is a bash script I wrote that does not work. It just puts the fat jar and the native executable in the dist/ directory before zipping them together.
FATJAR=blah-assembly-0.0.4.jar
mkdir -p dist/
rm -f dist/*
rm -f $DEPLOYED_ZIP
cp $FATJAR dist/
cp /usr/local/bin/ffmpeg dist/
(cd dist && zip -r $FATJAR ffmpeg && mv $FATJAR ../$DEPLOYED_ZIP)
Any structure of the zip file will do as long as the native binary included in the package appears in the PATH or is invoked through its full path. In both cases this means that you must update the Java code in one of the following ways:
modify the PATH environment variable or
call the binary by its full path.
As of this writing the path of the directory where the package contents is extracted on an AWS Lambda system can be read from the LAMBDA_TASK_ROOT environment variable.
I'm having trouble running the jar command in cygwin. The input-files parameter isn't treating the directory I'm passing it recursively when I'm referencing it with ".."s in my path.
For example, I'm running this in the same directory as the "src" directory. src/ contains my package structure of class and java files. This runs properly and creates a jar containing my source and class files.
jar cf jarname.jar src
However when I run this next command, I get an empty jar except for a manifest file.
jar cf jarname.jar localdir/../src
I need to run this from a script that needs to find this directory with a ".." directory so I need the 2nd command to work.
Anybody know why this isn't working or have a workaround? I tried using realpath but it complains that it can't find that path at all. I may be using it wrong though.
The Directory path in cygwin is different . To navigate to any drive for example to C drive we need to type in:
/cygdrive/c
A very easy work around i found useful is just to type cmd in the terminal . This allows you to use the actual path than the cygwin specific path .
try typing cmd and then running the command , it worked for me
How do u move a Jar file to the Start up folder with code? Like within the code either make a Jar file or move it to a different directory with Java.
Edit: So basicly i have a Jar file on the Desktop, I want to move the Jar file to or duplicate the jar file and move that to lets say C:\Program Files (x86) When u run the Jar File
I haven't tested this code, but if I remember correctly you want something along the lines of:
Runtime.getRuntime().exec("cp path/to/jar path/to/destination");
Where the string is the appropriate terminal command for your OS and what you want to do. Different methods surely exist, however I believe this is the easiest way. Here are some example commands:
Copying files:
Windows: cp path\to\jar destination\path
Linux: cp path/to/jar destination/path (note: you may need to prefix this command with sudo if the logged in user doesn't have the proper permissions. This can introduce its own headaches, so tread lightly)
Making a JAR archive:
Windows: jar cf path\to\jar path\to\files
Linux: jar cf path/to/jar path/to/files (again, it is possible you may need the sudo prefix)