How to include shell script in java
how to include a ".sh" file in a java code and then use it
Use
Runtime.getRuntime().exec();
Similar Messages
-
How to execute unix shell script from Java ...
Hi,
Anyone know how to execute unix shell script from Java?
Suppose I have several shell scripts written in perl or tcl or bash.
I just want to catch the output of that script.
Is there any ready to use module/object for this?
Please let me know, this is quite urgent for my study assigment.
Thanks in advance,
Regards,
meLook up Runtime.exec()
-
How to call a shell script in Java ?
Hi there,
i try to call a shell script in java but it wont work.
please can anybody help me.
my Code :
import java.io.*;
public class SendMessage{
Runtime rt;
public SendMessage(){
rt = Runtime.getRuntime();
public void send(){
try{
rt.exec("sendMessage.sh");
System.out.println("Message was Send");
catch(IOException ex)
ex.printStackTrace();
the file called sendMessage.sh is also stored in the same directory as the class File.
the exception never cames up but the script also never starts.
what is wrong ??? :-((
need help !
}rt.exec("sendMessage.sh");Oh, sorry, I didn't see that you tried under UNIX.
BTW you should check for the OS - this stuff is very os and os release dependent.
I have not experimented with this under unix.
But something must interpret that sendMessage.sh file.
Perhaps the hash bang info is used by exec() (did you put a
#!/bin/sh as first line in your .sh file?
If not, try
rt.exec("/bin/sh sendMessage.sh");Regards,
Marc -
How to invoke a shell script using java
Hi
I am trying to invoke a shell script(unix) using java and I have tryied to load the shell using exec(cmd) and then trying to execute the shell.
But it is not working.
Can you please guide me on this matter.Hi I am facing the same problem
I want to call a shell script through java in linux system of course.
I am using the following section of code .
public class LinuxServer {
public static void main(String args[]){
Runtime r = Runtime.getRuntime(); //get runtime information
try
Process Child = r.exec("/usr/bin/ksh") ; //execute command
BufferedWriter outCommand = new BufferedWriter(new OutputStreamWriter(Child.getOutputStream()));
outCommand.write("/opt/jboss-4.0.0/test1.sh");
outCommand.flush();
try
Child.waitFor(); //wait for command to complete
catch(InterruptedException e)
{ //handle waitFor failure
System.out.println("ERROR: waitFor failure");
System.exit(10); //exit application with exit code 10
catch(IOException e)
{ //handle exec failure
System.out.println("ERROR: exec failure"+e);
System.exit(11); //exit application with exit code 11
But when I am compiling and running this script in the linux system,
Its giving the following error.
Exception in thread "main" java.lang.NoClassDefFoundError: while resolving class: LinuxServer
at java.lang.VMClassLoader.resolveClass(java.lang.Class) (/usr/lib/libgcj.so.5.0.0)
at java.lang.Class.initializeClass() (/usr/lib/libgcj.so.5.0.0)
at java.lang.Class.forName(java.lang.String, boolean, java.lang.ClassLoader) (/usr/lib/libgcj.so.5.0.0)
at java.lang.Class.forName(java.lang.String) (/usr/lib/libgcj.so.5.0.0)
at gnu.gcj.runtime.FirstThread.run() (/usr/lib/libgcj.so.5.0.0)
at JvThreadRun(java.lang.Thread) (/usr/lib/libgcj.so.5.0.0)
at JvRunMain(java.lang.Class, byte const, int, byte const, boolean) (/usr/lib/libgcj.so.5.0.0)
at __gcj_personality_v0 (/opt/jboss-4.0.0/java.version=1.4.2)
at __libc_start_main (/lib/tls/libc-2.3.4.so)
at JvRegisterClasses (/opt/jboss-4.0.0/java.version=1.4.2)
Caused by: java.lang.ClassNotFoundException: java.lang.StringBuilder not found in [file:/usr/local/staf/lib/JSTAF.jar, file:/usr/local/staf/samples/demo/STAFDemo.jar, file:/home/db2inst4/sandip/staf/lib/JSTAF.jar, file:/home/db2inst4/sandip/staf/samples/demo/STAFDemo.jar, file:/usr/share/java/libgcj-3.4.3.jar, file:./, core:/]
at java.net.URLClassLoader.findClass(java.lang.String) (/usr/lib/libgcj.so.5.0.0)
at gnu.gcj.runtime.VMClassLoader.findClass(java.lang.String) (/usr/lib/libgcj.so.5.0.0)
at java.lang.ClassLoader.loadClass(java.lang.String, boolean) (/usr/lib/libgcj.so.5.0.0)
at JvFindClass(_Jv_Utf8Const, java.lang.ClassLoader) (/usr/lib/libgcj.so.5.0.0)
at java.lang.Class.forName(java.lang.String, boolean, java.lang.ClassLoader) (/usr/lib/libgcj.so.5.0.0)
at JvBytecodeVerifier.verify_instructions_0() (/usr/lib/libgcj.so.5.0.0)
at JvVerifyMethod(_Jv_InterpMethod) (/usr/lib/libgcj.so.5.0.0)
at JvPrepareClass(java.lang.Class) (/usr/lib/libgcj.so.5.0.0)
at JvWaitForState(java.lang.Class, int) (/usr/lib/libgcj.so.5.0.0)
at java.lang.VMClassLoader.linkClass0(java.lang.Class) (/usr/lib/libgcj.so.5.0.0)
at java.lang.VMClassLoader.resolveClass(java.lang.Class) (/usr/lib/libgcj.so.5.0.0)
...9 more
Pls help -
Invoking unix shell scripts from java?
Hi,
could someone explain to me how one wuld invoke unix shell scripts from java.
Also, could you invoke Visual Basic scripts from java.
Finally, could you do this from an EJB?
thanks for any help....
suduI just posted a snippet of this solution in the topic about widows commands chech it out it works just fine for unix shell scripts.
--Ian -
Running Unix Shell scripts through Java
How to run Unix shell scripts through Java program ?
Use:
Process p = Runtime.getRuntime().exec("sh script.sh");Then you can use:
p.getOutputStream and read the output of your program. -
Running a shell script through java
Hi all,
I have a simple question here.
How can I run a shell script through java and put the text output into a string.
I'd be very grateful if you could show me sample code...
Have a great day,
PeshoRuntime.exec()
There are plenty of examples. Read the following before continuing, however, as it will save alot of headaches:
http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html -
Invoking Shell Script from JAVA Stored procedure
I am trying to invoke shell script using Java Stored procedure.
When I run my java class outside of oracle using oracle user
account it works but as soon as i load it into database and try
to inovoke that class using PL/SQL wrapper it runs java class
but is not able to invoke the shell script. Any ideas would be
greatly appreciated.Pleass search the forums for "runtime" as there are many threads and examples already posted.
-
Hi,
Please let me know how to call shell scripts from WDA
Thanks
Bala DuvvuriHi Bala
You can not call any client side funcitons using Web Dynpro ABAP, it is possible in BSP using JavaScript.
You can use Suspend and Resume Plugs to navigate to a BSP page and perform the shell script and return to WDA using Resume Plug.
http://help.sap.com/saphelp_nw04s/helpdata/en/45/19bf8c16f25d7ae10000000a11466f/frameset.htm
Now, you can use JavaScript to execute shell script:
http://www.itjungle.com/mpo/mpo052302-story01.html
Abhi -
Invoking a bash shell script from Java code
Hi All
I am trying to invoke a Bash shell script using java code. The arguments required are "source wmGenPatch <source dir> <destination dir> no_reverse.
in the code I have specified the arguments considering the cannonical paths of the files as the code may run on Unix or windows platform.
I am getting a error while invoking Runtime.getRuntime().exec(args). The error is as follows :
"The Error Occurred is: CreateProcess: source D:\Package4.0\workspace\DiffEngineScripts\v4a02\wmGenPatch D:\Package4.0\workspace\fromImageFilesDir\ D:\Package4.0\workspace\toImageFilesDir\ no_reverse error=2"
It seems that error=2 indicates that the 'file not found' exception. But i can see the directories referred to in the error at place in the workspace.
Kindly advice.
Thanks in advance.Hi All
I am pretty new to invoking bash shell scripts from java and not sure if i am progressing in right direction.
The piece of code tried by me is as follows
try {
currentDir = f.getCanonicalPath();
} catch (IOException e) {
if (currentDir.contains("/")) {
separator = "/";
} else {
separator = "\\";
String args[] = new String[7];
args[0] = "/bin/sh";
args[1] = "-c";
args[2] = "source";
args[3] = currentDir + separator + "DiffEngineScripts" + separator
+ "v4a02" + separator + "wmGenPatch";
args[4] = sourceFileAdd;
args[5] = destFileAdd;
if (isReverseDeltaRequired) {
args[6] = "reverse";
} else {
args[6] = "no_reverse";
try {
Process xyz = Runtime.getRuntime().exec(args);
InputStream result = xyz.getInputStream();
InputStreamReader isr = new InputStreamReader(result);
BufferedReader br = new BufferedReader(isr);
String line = null;
while ( (line = br.readLine()) != null)
System.out.println(line);
int exitVal = xyz.waitFor();
System.out.println("Leaving Testrun.java");
} catch (Throwable t) {
t.printStackTrace();
and on running the same i am getting Java.io.IOException with the stack trace
java.io.IOException: CreateProcess: \bin\sh -c source D:\Package4.0\workspace\DiffEngineScripts\v4a02\wmGenPatch D:\Package4.0\workspace\fromImageFilesDir\ D:\Package4.0\workspace\toImageFilesDir\ no_reverse error=3
kindly advice
Thanks in advance -
How to run unix shell script from java web applet
hi all
i have created one java applet. my apache web server is on unix server.
i have created one shell script in same directory where my .class and .htm files reside...
how to run this shell script from applet? it should search this .sh file on server and not on the client browser machine...
thanks in advanceI suppose you could make the shell script into a CGI, configure the server to execute CGIs, and then make the applet open the URL of that CGI.
-
How do i print parameters in shell script through java
Hi,
I am sorry if have posted this query in wrong thread!!!
How do i print the parameter in shell script which are passed from java programm?
i have tried in different ways.. but the actual param value is not printing when i execute the java programm
java code is here
import java.io.IOException;
public class TestShell {
* @param args
public static void main(String[] args) {
String inputFilePath=args[0];
String inputFileName=args[1];
String outputFileName=args[2];
String outputFilePath=args[3];
Runtime r = Runtime.getRuntime();
String[] cmdArray = new String[]{"Test.sh",inputFilePath, inputFileName, outputFileName, outputFilePath};
try {
r.exec(cmdArray);
System.out.println("Test.sh executed successfully.");
} catch (IOException e) {
e.printStackTrace();
}I need a shell script to print paramenters (inputFilePath, inputFileName, outputFileName, outputFilePath)
Thanks,
Jasminuser13836688 wrote:
But my code is something look like thisWell unless I've completely lost my marbles (possible) or Java Runtime.exec() does something I've never heard of, I can't even see how that will execute. For a start, assignments in the Bourne shell take the form ofinfile = $inputFilename;orinfile = "inputFilename";not what you've got; and the first form would only work if you've set the appropriate environment variables.
Are you sure you're not confusing this with a Javascript script?
Winston
Edited by: YoungWinston on Apr 18, 2011 12:51 PM
And BTW, you don't need to put ';' at the end of each statement, unless there's more than one on a line.
Edited by: YoungWinston on Apr 18, 2011 12:54 PM
BTW2: 'printf "%d", inpath' also looks very dodgy, since 'inpath' is unlikely to be a number (in fact, it's unlikely to be anything).
I think you might want to get a book on scripting. -
How to flag shell script if there is a exception in java program
Hi all,
can someone let me know if it possible to flag the calling shell script that there is a exception in java program ???
Thanks
Chathave a question
In the shell script if I read for ret_stat=$? , will it give the value of 33 if exception occurs ? -
Pass in date from shell script to java program
Hi,
I need to pass in some parameter to my java program.
eg 'java SomeProgram yyyy mm dd hh mm ss'
How do i use the shell script to generate the valeus for the year, mth day etc?
The program is running once everyday.
I tried $argu0 = `date+%y`
java SomeProgram $argu0
but not working... thanks for your help!Then, why would you want to use 'date' to provide
'now' to a Java program when you could just use new
java.util.Date() which probably makes the same call
as the Unix 'date' command?That works until you need to use a date other than 'now.'
public static final String ISO_DATE = "yyyy-MM-dd HH:mm:ss";
public static void main(String[] args) {
SimpleDateFormat sdf = (SimpleDateFormat) SimpleDateFormat
.getDateTimeInstance();
sdf.applyPattern(ISO_DATE);
try {
Date d = sdf.parse(args[0]);
// rest of code here
} catch (ParseException e) {
// handle exception
}Users will have to enclose there input with quotes otherwise the JVM will consider their input as two parameters, or you can change the pattern to include a character between dd and HH. -
Calling Shell Script From Java
Hi i have a shell script which calls the ant command.How do i call this shell script from jdk 1.5. I used p = runtime.exec( filename) but it threw an IOException saying cannot execute. How do i call this from my java program which runs on the redhat linux box.Please Help
Possibility:
It does not have execute permissions - Either grant them by chmod or use the command as sh <script-name>
Rich
Maybe you are looking for
-
How can I publish an ics file to a remote server from my calendar server?
I am running Lion Server and have Calendar Service up and running. I have an account with a couple of calendars, one of which I'd like to publish to a remote server, as an .ics file. From an iCal client it is possible to publish local calendars, but
-
Hi. here i have to make the code in exit_saplv56u_004 in include zxv56u11. there are tables defined in itself. i have written like this,but not sure about the table updation and the declaration part: here i am writing this code in exit_sap TABLES:V
-
Transfering audio books from MacBook to iPhone 5s
I just asked how to transfer audio books from my MacBook to iPhone5s and had a reply that it should work like it did with my iPod touch. Well it doesn't. I tried moving audio books into my iTine Library (which I never did with iPod). I started to syn
-
Module & Image Mgr. not working
Below are the instructions for inserting a photo from the gallery. I have done it many times before. there is No image editor but a image manager - I did upload the images from my computer but it has no buttons to close it down, I have to refresh to
-
Reordering images in Survey mode
What are the restrictions on rearraging images in Survey mode? I could not figure out why it would sometines work and sometimes doesn't. For example, I have a main top directory 2012 and in it a folder for each month. If I am in GRID view at the 2012