Java Runtime.getRuntime().exec not returning any value


 
Thread Tools Search this Thread
Top Forums Programming Java Runtime.getRuntime().exec not returning any value
# 1  
Old 12-11-2014
Wrench Java Runtime.getRuntime().exec not returning any value

Hi,
i written class like this but it is not returning any results and infact p.waitFor() is returning value 1. This is very basic program that i am planning to run in unix. This is really killing my time and unable to find correct reason.
----------------------------------------------------------------------------------
Code:
import java.io.*;
public class myExecute {

    public static void main(String[] args) throws IOException{
        Process p=null;
        String filename;
        String cmd="/bin/touch -t 201412101053 /tmp/first && /bin/touch -t 201412101055 /tmp/last && /bin/find /opt/strms/NETEZZA_LOGS/mdo -type f -name retry.*.csv -newer /tmp/first ! -newer /tmp/last";
        int i;
         p = Runtime.getRuntime().exec(cmd);
         System.out.println("Executed:"+cmd);
         try {
            if(p.waitFor()==0){
                     System.out.println("waitfor is over");
                     i=p.exitValue();
                     System.out.println(i);
                     InputStream is = p.getInputStream();
                     InputStreamReader isr = new InputStreamReader(is);
                     BufferedReader br = new BufferedReader(isr);
                     while((filename=br.readLine())!=null){
                            System.out.println(filename);
                           
                     }
             }
            else{
                System.out.println(p.waitFor());
            }
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            System.out.println(e.toString());
            e.printStackTrace();
        }
        
    }
}

---------------------------------------------------------------
Appreciate for your help

Last edited by Franklin52; 12-12-2014 at 08:47 AM.. Reason: Please use code tags
# 2  
Old 12-11-2014
Make sure the command you are attempting to run is a valid shell command. Test this at the command line before running from code.

Code:
/bin/touch -t 201412101053 /tmp/first && /bin/touch -t  201412101055 /tmp/last && /bin/find /opt/strms/NETEZZA_LOGS/mdo  -type f -name retry.*.csv -newer /tmp/first ! -newer /tmp/last

# 3  
Old 12-11-2014
i have tested this command before putting in code. it is running fine
# 4  
Old 12-12-2014
First, you need to read both stdout and stderr streams from the process before you call waitFor(). If you don't, your child process could block/hang or even fail to run at all as it's output can't go anywhere. How do you read two streams and then call waitFor()? Multiple threads is the way I've seen it done most often.

Second, typing a command in an interactive shell isn't necessarily the same as running it as a subprocess. You may need to escape your "!" character in your Java string.

What's your operating system?
# 5  
Old 12-12-2014
I kept it simple and was able to run this code without error after I substituted CMD for something simpler like ls so it could very well be that you may need to quote special characters. The first thing that stood out to me in your program is the complexity of what you are shelling.
# 6  
Old 12-12-2014
Quote:
Originally Posted by blackrageous
I kept it simple and was able to run this code without error after I substituted CMD for something simpler like ls so it could very well be that you may need to quote special characters. The first thing that stood out to me in your program is the complexity of what you are shelling.
And that does not address why the original code failed.
# 7  
Old 12-12-2014
I think problem is in &&. if i just given "cat /tmp/ss.txt" then it is able to read and i am getting results back. but if i execute multiple commands by separating && then it is not working.
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Setting java and returning version

I am currently trying to write a script and I am not that great at wording it with taking in user input, it will do the following: ask " Have you stopped instances? " if instances are stopped, then I want it to change to the default java to 64 bits, and print the current version of java.... (1 Reply)
Discussion started by: bigbenn
1 Replies

2. Shell Programming and Scripting

Script Variables Inquiry, Values Okay in Standalone Exec, No-Show in Cron Exec

I have the following bash script lines in a file named test.sh. #!/bin/bash # # Write Date to cron.log # echo "Begin SSI Load $(date +%d%b%y_%T)" # # Get the latest rates file for processing. # d=$(ls -tr /rms/data/ssi | grep -v "processed" | tail -n 1) filename=$d export filename... (3 Replies)
Discussion started by: ginowms
3 Replies

3. Shell Programming and Scripting

exit status from ksh script exec from java using runtime

how do i get the exit status from a ksh or perl script executed in a java program using Runtime? (1 Reply)
Discussion started by: twk
1 Replies

4. UNIX for Dummies Questions & Answers

No Java runtime environment (JRE) error

Hi all, I am trying to install a .bin file for that it requires IBMJava2-AMD64-142-JRE-1.4.2-13.8.x86_64.rpm to be installed. I have installed this rpm but when i try to install .bin file, it complains that no JRE found. How to solve this. Thanks in advance! #... (0 Replies)
Discussion started by: lramsb4u
0 Replies

5. AIX

Java Runtime Execution require reboot of pSeries server regularly?

Dear all experts, Recently the daily batch run process (run using Java Runtime Execution)suddenly run slow. Our apps vendor came in and check and request to reboot the server. After rebooting of the server, the batch run back to normal. May I know is periodically rebooting of pSeries server is a... (9 Replies)
Discussion started by: kwliew999
9 Replies

6. UNIX for Dummies Questions & Answers

java runtime for mozilla firefox 3.5.3 installation

Hi, community, I am using Karmica koala 9.10 and I wish to add to Firefox the JRE plugin. What I have to do? The problem arises when I try to copy from the Downloads dir the file to /usr/java that I created. I must have the root permissions, I think. Please help. (1 Reply)
Discussion started by: mauropera
1 Replies

7. Programming

Runtime.getSystem.exec() function waits for child

Runtime.getSystem.exec() waits for child process to complete .. I do not want to wait for the child process to complete ..what is the option which has to be used ? (2 Replies)
Discussion started by: shafi2all
2 Replies
Login or Register to Ask a Question