Results 1 to 3 of 3
  1. #1
    baozhi is offline Member
    Join Date
    Mar 2010
    Posts
    1
    Rep Power
    0

    Default executing linux command from Java not working.

    I am trying to run linux sort from Java. but it's not working.

    Process p1 = Runtime.getRuntime().exec("/bin/sort -k5,5 -k1,1n in.txt > out.txt");
    int exitV = p1.waitFor();

    The out.txt never get generated. and exitV always return 2.

    I run the command in Linux, works fine.
    I run Process p1 = Runtime.getRuntime().exec("/bin/touch out.txt"); works fine. so has nothing to do with write permission.

    I've been struggle for a whole. please help!

  2. #2
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    Experiment with other Runtime.getRuntime().exec() methods, like the ones that take an array of arguments rather than a single line... in principle, what could be happening is that your call gets translated into something like

    /bin/sort "-k5,5 -k1,1n in.txt > out.txt"

  3. #3
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    When you exec a function from java, there is no shell in the loop. The ">" is a shell command, so this is not going to do what you expect. If you want to use shell commands, you have to exec a script that invokes the appropriate shell, and then exec the script.

    Java Code:
    contents of script 'theScript.csh'...
    
    #!/bin/csh -f
    
    /bin/sort -k5,5 -k1,1n in.txt > out.txt
    
    ...java exec command
    
    Process p1 = Runtime.getRuntime().exec("theScript.csh");

Similar Threads

  1. Executing a jar double-click style in Linux
    By goodwillwins in forum AWT / Swing
    Replies: 24
    Last Post: 03-19-2011, 05:56 AM
  2. problem in executing linux command
    By Basit56 in forum New To Java
    Replies: 1
    Last Post: 02-08-2010, 04:53 PM
  3. Executing external command through JSP
    By rudranee in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 03-21-2009, 10:22 AM
  4. linux command to start a java web server
    By lazarus in forum JavaServer Faces (JSF)
    Replies: 0
    Last Post: 06-24-2008, 04:30 PM
  5. calling linux command line in java
    By fangzhong in forum New To Java
    Replies: 0
    Last Post: 02-03-2008, 05:24 PM

Posting Permissions

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