473,388 Members | 1,426 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

Running an external program under Linux

Nepomuk
3,112 Expert 2GB
Hi!
I'm trying to run an external Program with
Expand|Select|Wrap|Line Numbers
  1. Process p = Runtime.getRuntime().exec("/bin/sh -c \"/bin/gzip -c /home/user/workspace/TarGz/pics.tar > pics.tar.gz\"");
  2. CleanStream cleanError = new CleanStream(p.getErrorStream(), "ERROR");
  3. CleanStream cleanOutput = new CleanStream(p.getInputStream(), "OUTPUT");
  4. clearError.start();
  5. clearOutput.start();
  6. p.waitFor();
  7.  
under Linux.
Here's the CleanStream.java:
Expand|Select|Wrap|Line Numbers
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.io.InputStreamReader;
  5.  
  6. public class ClearStream extends Thread {
  7.     InputStream is;
  8.     String type = null;
  9.     boolean typeSet = false;
  10.  
  11.     ClearStream(InputStream is)//, String type)
  12.     {
  13.         this.is = is;
  14.         //this.type = type;
  15.     }
  16.     ClearStream(InputStream is, String type)
  17.     {
  18.         this.is = is;
  19.         this.type = type;
  20.         typeSet = true;
  21.     }
  22.  
  23.     public void run()
  24.     {
  25.         try
  26.         {
  27.             InputStreamReader isr = new InputStreamReader(is);
  28.             BufferedReader br = new BufferedReader(isr);
  29.             String line=null;
  30.             while ( (line = br.readLine()) != null)
  31.             {
  32.                 System.out.print("");
  33.                 if(typeSet) System.out.println(type + "> " + line);
  34.             }
  35.         }
  36.         catch (IOException ioe)
  37.         {
  38.             ioe.printStackTrace();  
  39.         }
  40.     }
  41. }
  42.  
The command works fine, when used within bash, but from Java I get the error:
Expand|Select|Wrap|Line Numbers
  1. ERROR> -c: 1: Syntax error: Unterminated quoted string
  2.  
I checked the Runtime API and found, that exec(String command) has the token parsed with a StringTokenizer, which would explain, that the last quotation mark isn't found.

I tried packing the String into an array, to avoid this, by using exec(String[] cmdarray).
It looks like this then:
Expand|Select|Wrap|Line Numbers
  1. String[] use = {"/bin/sh -c \"/bin/gzip -c /home/user/workspace/TarGz/pics.tar > pics.tar.gz\""};
  2. Process p = Runtime.getRuntime().exec(use);
  3. // same as after here
  4.  
The error here is even worse:
Expand|Select|Wrap|Line Numbers
  1. java.io.IOException: Cannot run program "/bin/sh -c "/bin/gzip -c /home/user/workspace/TarGz/pics.tar > pics.tar.gz"": errir=2, No such file or directory
  2.    at java.lang.ProcessBuilder.start(ProcessBuilder.java:459)
  3.    at java.lang.Runtime.exec(Runtime.java:593)
  4.    at java.lang.Runtime.exec(Runtime.java:466)
  5.    at TarSomething.main(TarSomething.java:18)
  6. Caused by: java.io.IOException: java.io.IOException: error=2, No such file or directory
  7.    at java.lang.UNIXProcess.<init>(UNIXProcess.java:148)
  8.    at java.lang.ProcessImpl.start(ProcessImpl.java:65)
  9.    at java.lang.ProcessBuilder.start(ProcessBuilder.java:452)
  10.    ... 3 more
  11.  
(Line 18 in TarSomething is Process p = Runtime.getRuntime().exec(use);)

I've been trying to solve this problem for a while, but without success.

Can anyone help me?

Greetings,
Nepomuk
Sep 26 '07 #1
1 7514
Nepomuk
3,112 Expert 2GB
OK, Problem solved.
I used this:
Expand|Select|Wrap|Line Numbers
  1. String[] use = {"/bin/sh", "-c", "/bin/gzip -c /home/user/workspace/TarGz/pics.tar > pics.tar.gz"};
  2. // Same from this point
  3.  
Greetings,
Nepomuk
Sep 26 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Kathy Benson | last post by:
Hi, I need to open an external file, such as *.htm from a Java program. I need to open this file by the default program that handles this extension on the machine. So *.htm should always be opened...
3
by: Hal Vaughan | last post by:
I need to be able to run external commands from within a Java program, on Linux and Windows (and eventually OSX). Under most circumstances there is no problem, but if the path I specify as part of...
5
by: | last post by:
Maybe I ask something OS depended but maybe not. So I ask: I want to run an external program from my C++ program. I want stdin of external program constructed from a C++ string in my program....
0
by: Peter Chant | last post by:
I hope no one minds me running this past them. I'm running a linux machine with with apache, php and mysql. This is not accessable from the internet. I want a server that is visable to the...
10
by: I. Myself | last post by:
Suppose we spawn a child process with Popen. I'm thinking of an executable file, like a compiled C program. Suppose it is supposed to run for one minute, but it just keeps going and going. Does...
4
by: Dylan Parry | last post by:
Hi folks, I'm writing a program that needs to execute an external program and wait for it to finish running before it can make use of the output from that program. Specifically, the external...
4
by: holysmokes99 | last post by:
I have a VB6 application that references a few .Net 1.1 components, and one 2.0 component. Does that mean that all will be running under 2.0? Is there any way to force the 1.1 pieces to run under...
0
by: jfigueiras | last post by:
>I have a problem with the module subprocess! As many other programs... I'm not sure what you mean by "non-standard file descriptors". The other program is free to open, read, write, etc any...
9
by: Jimmy | last post by:
Well, i know it may be a little non-python thing, however, I can think of no place better to post this question :) can anyone tell me, in python, how to obtain some information of a running...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.