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
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.
I tried but it didn’t work…
rushiku
February 26, 2008, 1:45pm
4
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.
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…
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!!