469,356 Members | 2,055 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,356 developers. It's quick & easy.

Call Shell Script From java

Hi All,

i want to run shell script from java.shell script run successfully and generate a file.
But when i want to check the existence of that file,it shows that the file is not present.

please help me to check the file from java or how could i know the proccess(Shell script) is finished.

i have given my code below.

package com;

import java.io.*;
import java.util.*;


public class ScriptBuilder {
public ScriptBuilder() {
}

public void writeScript() throws java.io.IOException {
Runtime rt = Runtime.getRuntime();
rt.exec("chmod 777 testinglinux.sh");
Process p = rt.exec("./testinglinux.sh");
try {
p.waitFor();
} catch (Exception e) {
e.printStackTrace();
}
String shellfilename = "testing.txt";
if (new File(shellfilename).exists()) {
System.out.println(shellfilename+" File exists");
}
}

public static void main(String[] args) throws java.io.IOException {
ScriptBuilder sb = new ScriptBuilder();
sb.writeScript();
}
}

Thanks!
Vaskar
Mar 18 '08 #1
3 23366
r035198x
13,262 8TB
Hi All,

i want to run shell script from java.shell script run successfully and generate a file.
But when i want to check the existence of that file,it shows that the file is not present.

please help me to check the file from java or how could i know the proccess(Shell script) is finished.

i have given my code below.

package com;

import java.io.*;
import java.util.*;


public class ScriptBuilder {
public ScriptBuilder() {
}

public void writeScript() throws java.io.IOException {
Runtime rt = Runtime.getRuntime();
rt.exec("chmod 777 testinglinux.sh");
Process p = rt.exec("./testinglinux.sh");
try {
p.waitFor();
} catch (Exception e) {
e.printStackTrace();
}
String shellfilename = "testing.txt";
if (new File(shellfilename).exists()) {
System.out.println(shellfilename+" File exists");
}
}

public static void main(String[] args) throws java.io.IOException {
ScriptBuilder sb = new ScriptBuilder();
sb.writeScript();
}
}

Thanks!
Vaskar
There is a File.exists() method. Will that do for you?
Mar 18 '08 #2
r035198x
13,262 8TB
Having looked over your question again, I realize now that my response is meaningless.

What you need is this article which explains the traps one runs into when using Runtime.exec.
Mar 18 '08 #3
satch
23
Hi All,

i want to run shell script from java.shell script run successfully and generate a file.
But when i want to check the existence of that file,it shows that the file is not present.

please help me to check the file from java or how could i know the proccess(Shell script) is finished.

i have given my code below.
Expand|Select|Wrap|Line Numbers
  1. package com;
  2.  
  3. import java.io.*;
  4. import java.util.*;
  5.  
  6.  
  7. public class ScriptBuilder {
  8. public ScriptBuilder() {
  9. }
  10.  
  11. public void writeScript() throws java.io.IOException {
  12. Runtime rt = Runtime.getRuntime();
  13. rt.exec("chmod 777 testinglinux.sh");
  14. Process p = rt.exec("./testinglinux.sh");
  15. try {
  16. p.waitFor();
  17. } catch (Exception e) {
  18. e.printStackTrace();
  19. }
  20. String shellfilename = "testing.txt";
  21. if (new File(shellfilename).exists()) {
  22. System.out.println(shellfilename+" File exists");
  23. }
  24. }
  25.  
  26. public static void main(String[] args) throws java.io.IOException {
  27. ScriptBuilder sb = new ScriptBuilder();
  28. sb.writeScript();
  29. }
  30. }
  31.  
Thanks!
Vaskar
As I am in windows environment, I tried your program by changing the shell scrip to a batch file and it worked fine. I dont know any reason why it should fail for you.
To check the return status of the process use the exitValue method. A successful completion would return 0 exit value.
Mar 18 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

5 posts views Thread by Ayesha Ahsan | last post: by
4 posts views Thread by Christian | last post: by
2 posts views Thread by ellennolan | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.