I am trying to jar all the files in a folder with the jar command using Java as follows,
jar -cvf /Abhishek/logs.jar my_directory/logs/*.logs*
I am using Java to execute this.
String cmd[] = {"jar", "-cvf", "/Abhishek/logs.jar", "my_directory/logs/*.logs*"};
pb = Runtime.getRuntime().exec(cmd);
but only the manifest is getting stored in the jar.
added manifest
When I extract the jar file, I am getting the manifest file.
So I had a doubt regarding
*log.*
Is this type of syntax allowed to be used?
Cause it worked fine in the terminal.
Could someone shed some light into this?
Thank you.
On Linux, file globbing is done by the shell. So when you enter *.log at the terminal, the shell will expand it to the list of matching files. Which is then passed to the program.
If you execute the program directly, you would have to expand the pattern yourself. Or you could let the shell execute it with a command similar to
String cmd[] = {"sh", "-c", "jar -cvf /Abhishek/logs.jar my_directory/logs/*.logs*"};
You may want to just use the same classes in the java.util.jar package that the jar command uses. This will avoid a second Java process and will avoid relying on shell expansion:
try (JarOutputStream jar = new JarOutputStream(
new BufferedOutputStream(
Files.newOutputStream(
Paths.get("/Abhishek/logs.jar"))));
DirectoryStream<Path> dir = Files.newDirectoryStream(
Paths.get("my_directory/logs"), "*.logs*")) {
for (Path file : dir) {
BasicFileAttributes attr =
Files.readAttributes(file, BasicFileAttributes.class);
String name = file.toString();
if (attr.isDirectory()) {
name = name + "/";
}
JarEntry entry = new JarEntry(name);
entry.setLastModifiedTime(attr.lastModifiedTime());
entry.setLastAccessTime(attr.lastAccessTime());
entry.setCreationTime(attr.creationTime());
entry.setSize(attr.size());
jar.putNextEntry(entry);
Files.copy(file, jar);
}
}
Related
How could I run a local jar file from a java program?
The jar file is not in the class-path of the Java caller program.
I suggest you use a ProcessBuilder and start a new JVM.
Here is something to get you started:
ProcessBuilder pb = new ProcessBuilder("/path/to/java", "-jar", "your.jar");
pb.directory(new File("preferred/working/directory"));
Process p = pb.start();
Process proc = Runtime.getRuntime().exec("java -jar Validate.jar");
proc.waitFor();
// Then retreive the process output
InputStream in = proc.getInputStream();
InputStream err = proc.getErrorStream();
byte b[]=new byte[in.available()];
in.read(b,0,b.length);
System.out.println(new String(b));
byte c[]=new byte[err.available()];
err.read(c,0,c.length);
System.out.println(new String(c));
First, the description of your problem is a bit unclear. I don't understand if you want to load the classes from the jar file to use in your application or the jar contains a main file you want to run. I will assume it is the second.
If so, you have a lot of options here.
The simplest one would be the following:
String filePath; //where your jar is located.
Runtime.exec(" java -jar " + filepath);
Voila...
If you don't need to run the jar file but rather load the classes out of it, let me know.
Could something like the following be useful?
http://download.oracle.com/javase/tutorial/deployment/jar/jarclassloader.html
Another way to do on windows is:
Runtime.getRuntime().exec("cmd /c start jarFile");
this way you can set priority of your process as well (normal/low/etc)
You can run a jar file from where ever you want by using only this one line code.
Desktop.getDesktop().open(new File("D:/FormsDesktop.jar"));
where
new File("your path to jar")
Hope it helps.
Thanks.
Add jar library to your project
Import main class (see manifest in jar file)
Invoke static method main with arguments
String args[] = {"-emaple","value"};
PortMapperStarter.main(args);
To run an executable jar from inside your java application, you can copy the JarClassLoader from https://docs.oracle.com/javase/tutorial/deployment/jar/examples/JarClassLoader.java
Use it like this. In this snippet, jarUrl is the URL to download the jar from, for example file:/tmp/my-jar.jar and args is the array of strings you want to pass as command line arguments to the jar.
JarClassLoader loader = new JarClassLoader(jarUrl);
String main = loader.getMainClassName();
loader.invokeClass(main, args);
Keep in mind that you're now inserting someone else's binary into your code. If it gets stuck in an infinite loop, your Thread hangs, if it calls System.exit(), your JVM exits.
This is my appriach, which I consider is more complete:
public static Process exec(String path, String filename) throws IOException {
String javaHome = System.getProperty("java.home");
String javaBin = javaHome +
File.separator + "bin" +
File.separator + "java";
ProcessBuilder pb = new ProcessBuilder(javaBin, "-jar", path+filename);
return pb.start();
}
1) Set the class path from environment variables
2) Go to the folder where your jar file exists
3) Run the following commands through command prompt
java -jar jarfilename
I'm trying to unpack/extract an archive, which is supplied in my program, containing binaries.
The copy from within the jar to the file works just fine, but when I try to extract the zip, it returns unexpectedly and only copies half of a file, and ignores the other file completely.
Here's a bit more detailed description:
I'm trying to unzip an archive copied to a folder, from within the program's .jar.
The program I'm using to unzip is "unzip" (comes with Linux).
The command used to extract is:
unzip -o <file>.zip
which is exactly what I'm using in following code:
ProcessBuilder process = new ProcessBuilder();
process.command("unzip", "-o", adb_tools.toString());
process.redirectErrorStream(true);
Process pr = process.start();
String line;
BufferedReader processReader = new BufferedReader(new InputStreamReader(pr.getInputStream()));
while ((line = processReader.readLine()) != null)
log(Level.INFO, "Extracting Android Debugging Bridge: " + line, true);
log(Level.INFO, "Android Debugging Bridge has been extracted and installed to system. Marking files as executable...", true);
pr.destroy();
processReader.close();
When I use the command directly via the Terminal, everything works fine, both files are extracted and inflated, and are executable, however, as mentioned above, when I use the command in Java, only one file gets copied (and even that only goes half way), and the other file is completely ignored.
How can I fix this problem, and prevent this happening again, with different programs?
Thanks in advance!
If you need to do a common task in Java, there is always a library out there which does what you need better than yourself. So use an external library for unzipping. Check here:
What is a good Java library to zip/unzip files?
It looks like you can use zip4j like this (from djangofan's answer):
public static void unzip(){
String source = "some/compressed/file.zip";
String destination = "some/destination/folder";
String password = "password";
try {
ZipFile zipFile = new ZipFile(source);
if (zipFile.isEncrypted()) {
zipFile.setPassword(password);
}
zipFile.extractAll(destination);
} catch (ZipException e) {
e.printStackTrace();
}
}
I am running shell scripts with the help of java and cygwin. When i am running my code in windows xp it works fine. Now i am trying to run same code on windows 7 i am getting above error.
(java.io.IOException)java.io.IOException:
Cannot run program "sh" (in directory"c:\cygwin\bin\test"):
CreateProcess error=2.The system cannot find file specified
Why this error occurred.I have set my path for cygwin (PATH=.;c:\cygwin\bin) How to avoid this.
ProcessBuilder pb = new ProcessBuilder ();
pb.directory(new File("C:\\cygwin\\bin\\Test\\"));
File shellfile = new File("app.sh");//File name with extension
System.out.println(shellfile.getCanonicalPath());
But it is giving the output as E:\NIRAJ\example\app.sh which is in my java program. even i am setting up pb.directory to the path.
if i check System.out.print(pb.directory()); it gives me output C:\cygwin\bin\Test
In PATH variable, you need to put cygwin's bin directory before any other Windows' paths.
Do this:
PATH=c:\cygwin\bin:RestWindowsPaths
Not that:
PATH=RestWindowsPathVariables:c:\cygwin\bin
First try to get the path of specified file first to ensure it:
I am not much sure but this may lead you one step ahead :
File file = new File("app.sh");//File name with extension
System.out.println(file.getCanonicalPath());
This should print : c:\cygwin\bin\test
Also use separator like this instead : c:\\cygwin\\bin\\test
Hope this helps.
UPDATE
String myCommand = "c:\\cygwin\\bin\\test\\cygbin";
String myArg = PATH_TO_shellscript+"app.sh";
ProcessBuilder p = new ProcessBuilder(myCommand, myArg).start();
How could I run a local jar file from a java program?
The jar file is not in the class-path of the Java caller program.
I suggest you use a ProcessBuilder and start a new JVM.
Here is something to get you started:
ProcessBuilder pb = new ProcessBuilder("/path/to/java", "-jar", "your.jar");
pb.directory(new File("preferred/working/directory"));
Process p = pb.start();
Process proc = Runtime.getRuntime().exec("java -jar Validate.jar");
proc.waitFor();
// Then retreive the process output
InputStream in = proc.getInputStream();
InputStream err = proc.getErrorStream();
byte b[]=new byte[in.available()];
in.read(b,0,b.length);
System.out.println(new String(b));
byte c[]=new byte[err.available()];
err.read(c,0,c.length);
System.out.println(new String(c));
First, the description of your problem is a bit unclear. I don't understand if you want to load the classes from the jar file to use in your application or the jar contains a main file you want to run. I will assume it is the second.
If so, you have a lot of options here.
The simplest one would be the following:
String filePath; //where your jar is located.
Runtime.exec(" java -jar " + filepath);
Voila...
If you don't need to run the jar file but rather load the classes out of it, let me know.
Could something like the following be useful?
http://download.oracle.com/javase/tutorial/deployment/jar/jarclassloader.html
Another way to do on windows is:
Runtime.getRuntime().exec("cmd /c start jarFile");
this way you can set priority of your process as well (normal/low/etc)
You can run a jar file from where ever you want by using only this one line code.
Desktop.getDesktop().open(new File("D:/FormsDesktop.jar"));
where
new File("your path to jar")
Hope it helps.
Thanks.
Add jar library to your project
Import main class (see manifest in jar file)
Invoke static method main with arguments
String args[] = {"-emaple","value"};
PortMapperStarter.main(args);
To run an executable jar from inside your java application, you can copy the JarClassLoader from https://docs.oracle.com/javase/tutorial/deployment/jar/examples/JarClassLoader.java
Use it like this. In this snippet, jarUrl is the URL to download the jar from, for example file:/tmp/my-jar.jar and args is the array of strings you want to pass as command line arguments to the jar.
JarClassLoader loader = new JarClassLoader(jarUrl);
String main = loader.getMainClassName();
loader.invokeClass(main, args);
Keep in mind that you're now inserting someone else's binary into your code. If it gets stuck in an infinite loop, your Thread hangs, if it calls System.exit(), your JVM exits.
This is my appriach, which I consider is more complete:
public static Process exec(String path, String filename) throws IOException {
String javaHome = System.getProperty("java.home");
String javaBin = javaHome +
File.separator + "bin" +
File.separator + "java";
ProcessBuilder pb = new ProcessBuilder(javaBin, "-jar", path+filename);
return pb.start();
}
1) Set the class path from environment variables
2) Go to the folder where your jar file exists
3) Run the following commands through command prompt
java -jar jarfilename
ProcessBuilder pb = new ProcessBuilder("pwd");
pb.directory(new File("/server1/work/uz/rt/adapt/0/"));
Process s = pb.start();
I expected the output to be /server1/work/uz/rt/adapt/0/, but instead it's:
/work/uz/rt/adapt/0/
/work/uz/rt/adapt/0/ and /server1/work/uz/rt/adapt/0/ are equivalent (mounted at the same place,/work/.. is correct path and /server1/work/.. is the mounted one ), but I need to work under /server1/work/uz/rt/adapt/0/ because some other servers only work through that path.
How can I make /server1/work/uz/rt/adapt/0/ the current path?
IN OTHER WORDS
why public ProcessBuilder directory(File directory)
converts directory into canonical File. How can I use absolute File Path??
I also tried the hack soln'
pb.directory(new File("/asr1/work/oguz/rt/adaptMLLR2/0/"){
public File getCanonicalFile(){
return this.getAbsoluteFile();
}
public String getCanonicalPath() {
return this.getAbsolutePath();
}
});
which didnt work as well.
I resolved my problem by adding cd /server1/.. line in to the bash script.. and deleted pd.directory(..) line. BUT this problem (why I cant use pd.directory(..) with absolutePath ) is not answered yet...???
Using a shell's cd utility seems an appropriate solution. An alternative would be to set the PWD environment variable to the pathname with symlinks, but this is ugly unless you get ProcessBuilder to do it for you automatically.
Note that shells ignore PWD if it is not an absolute pathname for the current directory, asking the system for a absolute pathname without symlinks instead.