SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast
    Join Date
    Oct 2007
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Call Linux command from Java Application

    Hi all,

    i tried to run Linux command from Java Application.

    Here i past my code:-

    package com;

    public class linux_java {
    public static void main(String[] args) {
    try {
    String command = "cut -f 2,5 ABC/test.tab>ABC/test1.tab";
    final Process process = Runtime.getRuntime().exec(command);
    int returnCode = process.waitFor();
    System.out.println("Return code = " + returnCode);
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    }

    result:
    -------
    Return code =1

    But no new file was created.

    But when i run "cut -f 2,5 ALLIANCEHOURLYFILES/test.tab>ALLIANCEHOURLYFILES/test1.tab" command from Linux terminal it's works.

    please help me.

    Thanks!
    Vaskar

  2. #2
    SitePoint Addict ruby-lang's Avatar
    Join Date
    Aug 2007
    Posts
    389
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try using the full path to cut (probably /bin/cut or /usr/bin/cut), since if I'm not mistaken Java doesn't know it's supposed to look for your program in your PATH.

    Also make sure your program is running on the right directory.

  3. #3
    SitePoint Enthusiast
    Join Date
    Oct 2007
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I tried but it didn't work...

  4. #4
    SitePoint Wizard silver trophy rushiku's Avatar
    Join Date
    Dec 2003
    Location
    A van down by the river
    Posts
    2,056
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)
    Add
    System.out.println( System.getProperty("user.dir") );
    to get the current working directory, ABC should be a subdirectory of it.

    Alternately, use absolute paths for everything.

  5. #5
    SitePoint Enthusiast
    Join Date
    Oct 2007
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I changed my code

    package com;

    import java.io.*;

    public class linux_java {
    public static void main(String[] args) {
    try {
    String command =
    "/bin/cut -f 2,5 /var/www/html/final_java/ABC/qavhourly.tab";
    BufferedWriter out = new BufferedWriter(new FileWriter(
    new File(
    "/var/www/html/final_java/ABC/testqavhourly.tab"), true));
    final Process process = Runtime.getRuntime().exec(command);
    BufferedReader buf = new BufferedReader(new InputStreamReader(
    process.getInputStream()));
    String line;
    while ((line = buf.readLine()) != null) {
    out.write(line);
    out.newLine();
    }
    buf.close();
    out.close();
    int returnCode = process.waitFor();
    System.out.println("Return code = " + returnCode);
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    }

    the code is works properly.But it takes long time.Because the file size is too large i.e nearly 1 GB.Is there any way to reduce the time? pls help me.....

  6. #6
    SitePoint Member
    Join Date
    Feb 2010
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Exclamation

    I need to execute Linux commands in terminal with the help of Java GUI . I also need to store the result back to a file.Please help!!


Bookmarks

Posting Permissions

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