Results 1 to 4 of 4
  1. #1
    madhu_sharan is offline Member
    Join Date
    Jul 2012
    Posts
    2
    Rep Power
    0

    Default 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 = 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

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


    :(:(:(

    below is my java code snippet that i use,

    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.

  2. #2
    kammce's Avatar
    kammce is offline Senior Member
    Join Date
    Dec 2010
    Location
    California
    Posts
    194
    Rep Power
    4

    Default Re: running unix command from java

    Have you tried using this line of code? :

    Java Code:
     Process child = Runtime.getRuntime().exec(command);
    If you do not know how to use this properly, a quick google search will provide you with a bunch of examples of how to use this.

    This site had a good one as well.
    Running system commands in Java applications | java exec example | devdaily.com

    I would try to execute you command, but I would not want to access your ssh server.
    My API:
    Java Code:
    cat > a.out || cat > main.class

  3. #3
    madhu_sharan is offline Member
    Join Date
    Jul 2012
    Posts
    2
    Rep Power
    0

    Default Re: running unix command from java

    Quote Originally Posted by kammce View Post
    Have you tried using this line of code? :

    Java Code:
     Process child = Runtime.getRuntime().exec(command);
    If you do not know how to use this properly, a quick google search will provide you with a bunch of examples of how to use this.

    This site had a good one as well.
    Running system commands in Java applications | java exec example | devdaily.com

    I would try to execute you command, but I would not want to access your ssh server.
    I found out the solution.

    initially i used
    Java Code:
    ProcessBuilder procBuild=new ProcessBuilder(strShellCmd);
    but when i used
    Java Code:
    ProcessBuilder procBuild=new ProcessBuilder("bash","-c",strShellCmd);
    it worked :)

    thanks for your help,
    Madhu.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,172
    Rep Power
    20

    Default Re: running unix command from java

    Quote Originally Posted by kammce View Post
    Have you tried using this line of code? :

    Java Code:
     Process child = Runtime.getRuntime().exec(command);
    I'm guessing that is the equivalent of the line the OP has given, where runEnv is the Runtime and procTemp is a Process.

    For the OP all I can suggest is going through this article at JavaWorld which covers a lot of things you need to know about how Runtime exec() works (and sometimes doesn't).
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Running object-oriented in Unix
    By justOne in forum New To Java
    Replies: 3
    Last Post: 03-28-2012, 09:22 PM
  2. running command prompt using java
    By xclaim in forum New To Java
    Replies: 1
    Last Post: 01-14-2010, 04:46 AM
  3. Replies: 0
    Last Post: 06-20-2008, 10:02 AM
  4. Filter a file in java (unix cut command equivalent)
    By marcosabel in forum New To Java
    Replies: 0
    Last Post: 02-11-2008, 08:26 PM
  5. Running java program with arguments in Unix"
    By gvi in forum Advanced Java
    Replies: 2
    Last Post: 11-08-2007, 08:01 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •