Programming This 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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
|
 |
09-20-2004, 06:03 AM
|
#1
|
Member
Registered: Jul 2003
Location: NSW. Australia
Distribution: Ubuntu, Fedora, Slackware
Posts: 181
Rep:
|
java Runtime class help needed
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?
Cheers.
|
|
|
09-20-2004, 05:23 PM
|
#2
|
Senior Member
Registered: Oct 2003
Location: Zurich, Switzerland
Distribution: Debian/unstable
Posts: 1,357
Rep:
|
You don't wait for the process to finish, nor do you examine the contents of the stdout of the process.
|
|
|
09-20-2004, 10:20 PM
|
#3
|
Senior Member
Registered: Jun 2004
Posts: 2,553
Rep:
|
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.
|
|
|
09-20-2004, 11:29 PM
|
#4
|
LQ Guru
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Rep: 
|
Your program works fine...
Hi -
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":
Code:
rw-r--r-- 1 paulsm users 929 2004-09-20 21:24 DoExec.class
-rw-r--r-- 1 paulsm users 1 2004-09-20 21:18 file.1.txt
-rw-r--r-- 1 paulsm users 1 2004-09-20 21:18 file.2.txt
-rw-r--r-- 1 paulsm users 1 2004-09-20 21:18 file.3.txt
|
|
|
09-21-2004, 05:31 AM
|
#5
|
Member
Registered: Jul 2003
Location: NSW. Australia
Distribution: Ubuntu, Fedora, Slackware
Posts: 181
Original Poster
Rep:
|
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?
|
|
|
All times are GMT -5. The time now is 11:30 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|