473,666 Members | 2,296 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Running an external program under Linux

Nepomuk
3,112 Recognized Expert Specialist
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.jav a:
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.getRunt ime().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 7573
Nepomuk
3,112 Recognized Expert Specialist
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
10380
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 by Netscape if that is the default browser on the machine. I figured out how to do this on NT but could not find an equivalent command on Linux. Here is what I did on NT.
3
8003
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 the command has any spaces in it, or an argument for the command is a path with a space in it, or if I redirect the output to a file with a space in the filename, it doesn't work on Linux. I tried putting the paths in both single and double...
5
8991
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. I want stdout of external program constructs a C++ string in my program. Is this possible without intermediate temporary files?
0
1546
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 internet. For that purpose I am running a user mode linux machine and using apache on that as the external looking web server. This means that stuff I want to see on the outside is on a copy of apache that is doing little and does not get messed...
10
2990
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 Python have any way to kill it? This is not hypothetical; I'm doing it now, and it's working pretty well, but I would like to be able to handle this run-on condition. I'm using Windows 2000, but I want my program to be portable to linux. ...
4
8405
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 program converts from one file format to another, and is something that I can't do natively :( So what I need to figure out is how to: a) Start the external program, with parameters
4
1269
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 1.1, and the 2.0 to run under 2.0? I know about setting a config file to force a certain framework, but I think that is only good for the entire application. Thanks
0
1376
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 file he wants - are you trying to trap any file operation it may want to do? You *could* do such things -google for "code injection" and "API hooking"- but I doubt it's what you really want.
9
2178
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 program? paticularly, if i am playing some music in audacious or other media player, how can i get the the name and some other info of current playing song? It seems that audicious doesn't output on run-time
0
8866
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8638
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7381
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6191
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5662
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4193
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4365
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2769
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1769
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.