ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hi all,
I have read a bit about how people use the Runtime class improperly... and I'm now one of them!
Basically, I need to run a few Linux system commands, such as "gzip" and "tar" from within Linux, but I'm not getting any result at all.
here's my code:
Code:
String expand = "tar -xzvf " + file_to_expand; //where expand the full path to a filename
System.out.println(expand);
Runtime r = Runtime.getRuntime();
Process p = null;
try {
p = r.exec(expand);
System.out.println("Opening");
}
catch (Exception e) {
System.out.println("Error, argh");
}
This gives me:
Code:
tar -xzvf /home/stevo/Desktop/sources/misc.tar
Opening
but nothing. Its getting inside the try block, but not doing much... ie the tar archive isn't expnded. Is there something I missed? I read something about catching the output, but how can I do this?
let's see
you shouldn't have to catch the expanding archive
you can catch stdout and stderr though
this should catch the errors if there are any
(might help)
also might help not to get the runtime early but i don't know
wonder how many mistakes i made
Code:
String expand = "tar -xzvf " + file_to_expand; //where expand the full path to a filename
System.out.println(expand);
Process p;
InputStream is;
BufferedReader br;
try {
p = Runtime.getRuntime().exec(expand);
System.out.println("Opening");
// i thing stdout is getOutputStream();
is = p.getErrorStream();
br = new BufferedReader(new InputStreamReaded(is));
int c;
boolean what = true;
while (what) {
c = br.read();
if (c==-1) {
what = false;
}
System.out.print((char)c);
}
is.close();
br.close();
}
catch (Exception e) {
System.out.println("Error, argh");
}
Last edited by foo_bar_foo; 09-20-2004 at 10:30 PM.
Your program should in fact be doing exactly what you asked it to: extract the specified archive:
1. Here's my version of your code:
Code:
import java.*;
public class DoExec {
static final String expand = "tar xvf /tmp/tmp.tar";
public static void main (String[] args) {
System.out.println(expand);
Runtime r = Runtime.getRuntime();
Process p = null;
try {
p = r.exec(expand);
System.out.println("Opening");
}
catch (Exception e) {
System.out.println("Error, argh: " + e.getMessage ());
}
}
}
2. Here's the output I got (same as the output you got - basically, nothing. Because you're not trapping stdout. And, of course, you don't *need* to if you don't want to.):
Code:
/tmp/temp> java DoExec
tar xvf /tmp/tmp.tar
Opening
3. And finally, here are the files I extracted from running "DoExec":
Damn, how come mine isn't working?
Could it be the string I am passing? (expand) ?
Hang on, I just typed the expand string into the console:
Code:
stevo@h4x0r:~$ /bin/tar -xvf /home/stevo/Desktop/sources/misc.tar
i live at home in a trailer.txt
linux behind magic of shrek2.html
my geek code.txt
BUT, the tar file is STILL NOT EXPANDED! Looks as though its just looking inside the file, but not expanding it.
I then omit the "/bin/" and it still doesn't work. Same output, but no result! Am I missing something completely obvious?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.