473,908 Members | 6,609 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 23619
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
14417
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
12092
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
4121
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 knows will do the job) to call another .py script like the original shell script does.
3
5462
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 a shell script from a python script? and how do I do that such that control is tranferred to the...
2
3485
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 returns, so it cannot tell whether the shell script successfully performs or not. Even there is...
1
1985
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 java app, with no issues. The problem is when I fire the java app from the perl script using the...
2
3256
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
4688
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
6243
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
10031
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9875
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
11042
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9721
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8094
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5930
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4770
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
2
4336
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3355
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.