Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 07-09-2012
Registered User
 
Join Date: Jul 2012
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
[SOLVED] running unix command from java

Hi All,

I have been struggling for a week trying to run a unix command from my java program.

the unix command is:

Code:
ssh dmdev3@tsapid01-zt3d01 ':> /t3/envs/dmdev3/test/file_list.txt'

when i try to run this command directly on my unix console, it works perfectly.

but when i try it form my JAVA program, i get the below mentioned error


Code:
ksh: :> /t3/envs/dmdev3/test/file_list.txt:  not found



below is my java code snippet that i use,

Code:
procTemp=runEnv.exec(strShellCmd);

where strShellCmd contains the entire unix command as a single string


requiring your help at the earliest cause i'm in such a desperate situation.

Thanks in advance,
Madhu.


Moderator's Comments:
Please use code tags next time for your code and data.

Last edited by zaxxon; 07-09-2012 at 07:56 AM.. Reason: code tags, see PM!
Sponsored Links
    #2  
Old 07-09-2012
Registered User
 
Join Date: Sep 2011
Posts: 83
Thanks: 2
Thanked 10 Times in 10 Posts
Please don't cross post the same question across multiple forums, it dilutes effort.
Sponsored Links
    #3  
Old 07-09-2012
Registered User
 
Join Date: Jul 2012
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
i am sorry. was a bit confused on the correct location of the topic.
rest assured it wont happen again.
    #4  
Old 07-09-2012
methyl methyl is offline Forum Advisor  
Advisor
 
Join Date: Mar 2008
Posts: 6,388
Thanks: 286
Thanked 668 Times in 640 Posts
Quote:
where strShellCmd contains the entire unix command as a single string
Let's see that line too.
First impression is that the command would be better placed in Shell Script.

What (in words) are you expecting the command to do. It appears to just execute a null command on a remote server.
Sponsored Links
    #5  
Old 07-09-2012
ɹǝsn sıɹɐlos
 
Join Date: Dec 2007
Location: Paris
Posts: 4,187
Thanks: 14
Thanked 409 Times in 376 Posts
The /t3/envs/dmdev3/test/file_list.txt file is (should be) cleared by this command.
Sponsored Links
    #6  
Old 07-09-2012
Registered User
 
Join Date: Jul 2012
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts

Code:
private boolean executeShell(String strShellCmd) {
        try {
            procTemp=runEnv.exec(strShellCmd);

            //read the output from shell console
            BufferedReader brShellOP = new BufferedReader(
                    new InputStreamReader(procTemp.getInputStream()));
            String strShellOP = null;
            while ((strShellOP = brShellOP.readLine()) != null) {
                System.out.println(strShellOP);
            }
            brShellOP.close();

            // catch error from shell console (if any)
            BufferedReader brShellError = new BufferedReader(
                    new InputStreamReader(procTemp.getErrorStream()));
            String strShellError = null;
            while ((strShellError = brShellError.readLine()) != null) {
                System.out.println(strShellError);
            }
            brShellError.close();

            // wait for the process to finish
            try {
                intExitVal = procTemp.waitFor();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
            
            //returns false if shell console throws error
            if(intExitVal!=0){
                return false;
            }
            return true;

        } catch (IOException ioEx) {
            System.out.println("error in executeShell().IOException");
            ioEx.printStackTrace();
            return false;
        }
    }

this is the entire method that does the execute operation.
Let me know if you require any other inputs for getting my issue solved.

PS: i'm new to this executing-unix-from-java thing.

Thanks a tonne,
Madhu.
Sponsored Links
    #7  
Old 07-09-2012
Peasant's Avatar
Registered User
 
Join Date: Mar 2011
Posts: 524
Thanks: 15
Thanked 110 Times in 108 Posts
Use cp /dev/null /t3/envs/dmdev3/test/file_list.txt
Sponsored Links
Closed Thread

Tags
java, unix

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
running unix command from java madhu_sharan UNIX for Advanced & Expert Users 0 07-09-2012 06:48 AM
[Solved] save the return value of a unix command peuceul Shell Programming and Scripting 3 12-01-2011 09:13 AM
Exception Signal 11 while running JAVA code in UNIX satish2712 Programming 0 08-11-2008 02:08 AM
exit status running java classpath in unix shell mmcds Programming 2 08-02-2007 10:06 PM



All times are GMT -4. The time now is 07:43 PM.