LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 09-20-2004, 06:03 AM   #1
ludeKing
Member
 
Registered: Jul 2003
Location: NSW. Australia
Distribution: Ubuntu, Fedora, Slackware
Posts: 181

Rep: Reputation: 30
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.
 
Old 09-20-2004, 05:23 PM   #2
ToniT
Senior Member
 
Registered: Oct 2003
Location: Zurich, Switzerland
Distribution: Debian/unstable
Posts: 1,357

Rep: Reputation: 47
You don't wait for the process to finish, nor do you examine the contents of the stdout of the process.
 
Old 09-20-2004, 10:20 PM   #3
foo_bar_foo
Senior Member
 
Registered: Jun 2004
Posts: 2,553

Rep: Reputation: 53
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.
 
Old 09-20-2004, 11:29 PM   #4
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
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
 
Old 09-21-2004, 05:31 AM   #5
ludeKing
Member
 
Registered: Jul 2003
Location: NSW. Australia
Distribution: Ubuntu, Fedora, Slackware
Posts: 181

Original Poster
Rep: Reputation: 30
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?
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Java Programming: Java Runtime Environment not found when trying to compile murbz Linux - Software 2 03-26-2009 03:04 AM
guidance needed please with java runtime ceborame Ubuntu 2 08-18-2005 01:49 PM
Compile Java - .class, .java, .jar ? woranl Programming 2 11-09-2004 10:12 PM
java runtime shariqali Linux - Software 7 09-23-2003 10:08 PM
Java Runtime Env. Ratclaws Linux - Software 1 07-03-2002 07:52 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:29 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration