How to Extract a Jar in a Unix Filesystem with a Single Command and Specify Its Target Directory Using the Jar Command

How do you extract a JAR in a UNIX filesystem with a single command and specify its target directory using the JAR command?

If your jar file already has an absolute pathname as shown, it is particularly easy:

cd /where/you/want/it; jar xf /path/to/jarfile.jar

That is, you have the shell executed by Python change directory for you and then run the extraction.

If your jar file does not already have an absolute pathname, then you have to convert the relative name to absolute (by prefixing it with the path of the current directory) so that jar can find it after the change of directory.

The only issues left to worry about are things like blanks in the path names.

Want to extract a specfic jar file from an ear file and store it in a specific directory

It is not possible to extract file to specific location using jar.
jar tool accepts 2 arguments:

  1. archive file
  2. names of archived files to extract

so you cannot extract it to specific location.

Try to extract files with some command line script, that will do extraction first, and then copy file to specific location.

Unjar to a specific destination

I don't think jar supports this, but unzip does. Try:

unzip my.jar -d bar

The behaviour is the same as the jar command, because jar files are the same as zip files.

Unpack all jar and war files with in a zip file

Try below method:

public void unzipFile(String zipFile) throws ZipException, IOException 
{
int BUFFER = 2048;
ZipFile zip = new ZipFile(new File(zipFile));
String pathToMainFile = zipFile.substring(0, zipFile.length() - 4);
new File(pathToMainFile).mkdir();
Enumeration zipEntries = zip.entries();
while (zipEntries.hasMoreElements())
{
ZipEntry entry = (ZipEntry) zipFileEntries.nextElement();
String current = entry.getName();
File dest = new File(pathToMainFile, current);
File outerParentSt = dest.getParentFile();
outerParentSt.mkdirs();

if (!entry.isDirectory())
{
BufferedInputStream is = new BufferedInputStream(zip.getInputStream(entry));
int currentByte;
byte data[] = new byte[BUFFER];
// write the file
FileOutputStream fos = new FileOutputStream(dest);
BufferedOutputStream destbuff = new BufferedOutputStream(fos,
BUFFER);

// r w to EOF
while ((currentByte = is.read(data, 0, BUFFER)) != -1) {
destbuff.write(data, 0, currentByte);
}
dest.flush();
dest.close();
is.close();
fos.close;
}

if (current.endsWith(".zip")) //or jar or war
{
unzipFile(dest.getAbsolutePath()); //recursively
}
}
}

Is there a quick way to delete a file from a Jar / war without having to extract the jar and recreate it?


zip -d file.jar unwanted_file.txt

jar is just a zip file after all. Definitely much faster than uncompressing/recompressing.

How to list only folders inside jar using jar cmmand similar to jar -tvf will give all files and directory?

Command line(Linux);

jar tf JAR_PATH | grep ".*/$"

Java code to take directories in a jar file;

try {
JarFile jarFile = new JarFile(JAR_PATH);
Enumeration<JarEntry> paths = jarFile.entries();
while (paths.hasMoreElements()) {
JarEntry path = paths.nextElement();
if (path.isDirectory()) {
System.out.println(path.getName());
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

How to extract / unpack / unzip hudson.war in specified directory Linux / Windows and not on default?

I understand, such situation arises when you have limited space under your home directory.

Here are the steps and explanation for Windows and Linux

Windows:

By default
java -jar hudson.war will extract .war on path %USERPROFILE%\.hudson
For example in my case, in command terminal:

C:\Hudson>echo %USERPROFILE%\.hudson
C:\Users\osao.ORADEV\.hudson

This is the path where hudson.war is extracted.
For extracting in userdefined directory, you need to set HUDSON_HOME environment variable

To set persistent environment variables: Right click My Computer ==> Properties ==> Advanced system settings ==> Environment Variables ==> System variables New.
Make sure that HUDSON_HOME is not already there. If yes, delete it.
Add HUDSON_HOME: <your specified directory>

Now open/restart command terminal. Check echo %HUDSON_HOME% it must be updated HUDSON_HOME

Now invoke
java -jar hudson.war from the path where hudson.war is there.
You will notice that extraction is in specified directory.

Linux:

By default extraction happens in $HOME
You need to set $HUDSON_HOME
Based on your SHELL: csh or bash

csh:

mkdir /home/osao/myHudsonHome
setenv HUDSON_HOME /home/osao/myHudsonHome
echo $HUDSON_HOME
java -jar hudson.war

bash:

mkdir /home/osao/myHudsonHome
export HUDSON_HOME=/home/osao/myHudsonHome
echo $HUDSON_HOME
java -jar hudson.war

I hope this will help.

Find a class somewhere inside dozens of JAR files?

Eclipse can do it, just create a (temporary) project and put your libraries on the projects classpath. Then you can easily find the classes.

Another tool, that comes to my mind, is Java Decompiler. It can open a lot of jars at once and helps to find classes as well.



Related Topics



Leave a reply



Submit