473,574 Members | 2,692 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Call Shell Script From java

27 New Member
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.IOExcep tion {
Runtime rt = Runtime.getRunt ime();
rt.exec("chmod 777 testinglinux.sh ");
Process p = rt.exec("./testinglinux.sh ");
try {
p.waitFor();
} catch (Exception e) {
e.printStackTra ce();
}
String shellfilename = "testing.tx t";
if (new File(shellfilen ame).exists()) {
System.out.prin tln(shellfilena me+" File exists");
}
}

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

Thanks!
Vaskar
Mar 18 '08 #1
3 23607
r035198x
13,262 MVP
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.IOExcep tion {
Runtime rt = Runtime.getRunt ime();
rt.exec("chmod 777 testinglinux.sh ");
Process p = rt.exec("./testinglinux.sh ");
try {
p.waitFor();
} catch (Exception e) {
e.printStackTra ce();
}
String shellfilename = "testing.tx t";
if (new File(shellfilen ame).exists()) {
System.out.prin tln(shellfilena me+" File exists");
}
}

public static void main(String[] args) throws java.io.IOExcep tion {
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 MVP
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 New Member
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

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

Similar topics

5
14393
by: Ayesha Ahsan | last post by:
Hi, I use Runtime.getRuntime().exec(command) to make my system call. For Windows based Dos, i add "cmd /c" before I type in my system call. So for example make the system call "dir": String command="cmd /c dir"; Runtime.getRuntime().exec(command); What is the equivalenr of cmd /c in Unix .
4
12063
by: Laura P | last post by:
Hi, I wasn't sure whether this should be posted in the Java are or in a Solaris thread, so I shall post it in both. Sorry for the duplication. I am new to Solaris and am having trouble running a long Java command from a shell script. Firstly, my Solaris (8) / Java setup. Mu machine already had Java 1.2
4
4109
by: Christian | last post by:
From a not even newbie: Without knowing much about Python (yet) I'm trying to install the CMS Zope via FTP (with the well documented changes to make it work on an Apache server). By birth Zope is started from a shell script. And not having the permissions to execute such ones I'll try writing a .py script (with the shebang that I allready...
3
5443
by: telduivel | last post by:
Can someone please help me with this: I have a python script, that at some point calls a linux bash script (.sh). Starting the shell script is the last thing my python script needs to do, so I would like for the python script to exit once the call has been made, without waiting for the shell script to finish. My question is: how do I call...
2
3472
by: ellennolan | last post by:
Hello, I wonder if anyone can help with calling external shell script in c++. Basically, in the shellscript, I want to pass src, dst, md5. If the src's md5 matches md5 given, it will be link to the dst, if ln fails, copy src to dst. If success, return 0, if fails, return 1. I tried both system, and execvp(fork) to work on it. As execvp never...
1
1971
by: houh | last post by:
I have a java app that generates openoffice docs. When I run the java app in a shell script, as: export JAVA_HOME=/opt/corp/projects/metrics/Java/jdk1.5.0_07/bin/java export CONST_LOC=/home/methct/metrics/MGConfig/staging_8103.xml $JAVA_HOME view.main.MetricsMainFrame $CONST_LOC I can close the openoffice doc that are generated by the...
2
3239
by: smitanaik | last post by:
i have a java file which i am running through shell script. the syntax that i have used is #! /bin/bash javac Copy.java
2
4669
by: ashutoshrawat | last post by:
Hi I had 2 AIX system.i want to run a shell script on one machine, which will execute another shell script on another AIX machne. the 2nd remotely executed shell script will execute a java programe on 2nd AIX machine i had gone on some similar thread in this forum.... which includes ssh http://bytes.com/forum/thread579549.html
7
6220
by: Samuel A. Falvo II | last post by:
I have a shell script script.sh that launches a Java process in the background using the &-operator, like so: #!/bin/bash java ... arguments here ... & In my Python code, I want to invoke this shell script using the Subprocess module. Here is my code: def resultFromRunning_(command):
0
7814
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7736
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8067
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8107
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5631
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5307
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3748
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2252
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1071
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.