aspose file tools*
The moose likes Java in General and the fly likes Callling a Script from a java program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Callling a Script from a java program" Watch "Callling a Script from a java program" New topic
Author

Callling a Script from a java program

Gobind Singh
Ranch Hand

Joined: Aug 04, 2006
Posts: 62
Does anyone know how I can call a bash shell script from a java program?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

Check out the "exec()" family of methods in the java.lang.Runtime class.


[Jess in Action][AskingGoodQuestions]
Gobind Singh
Ranch Hand

Joined: Aug 04, 2006
Posts: 62
Thanks. I've got the following program to work:

try {
Process proc = Runtime.getRuntime().exec(scriptPath);
int exitVal = proc.waitFor();
System.out.println("exit val = " + exitVal);
} catch (Throwable e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

This program basically invokes a script.

My question is can I return exit codes from a script to indicate success or failure?
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Yes, you can. Look up the documentation for bash, or whatever shell you're using.

Note that your Java code to execute the process is probably not sufficient. Unless you are very sure that your spawned process will not write to standard error or standard output (even under error conditions), you must arrange to consume these streams. Otherwise, your spawned process may hang. The documentation of java.lang.Process and Runtime.exec() has more on this.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Gobind Singh
Ranch Hand

Joined: Aug 04, 2006
Posts: 62
Also, How do I execute a script on a remote machine using my java program?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

Originally posted by Gurps Bassi:
Also, How do I execute a script on a remote machine using my java program?


Have you ever heard the joke about the woman who walks into a florist, and says "I'd like a big bouquet of roses, please" and the florist hands them to her, and she says "And add some irises, and maybe some of those, and a few of these, and a ribbon that says "CONGRATULATIONS", and here, let me write something on the card." And the florist says "Yes, Madame. Will that be all?" And the woman says "That's perfect! Now wire it to Chicago!"

Anyway, executing a script on a remote machine means 1) There's some code on the remote machine that will let you contact it over the network and execute the script, be it an rsh or ssh service, a Web or application server, an RMI service, etc; and 2) You have to write the code to contact that code and ask it to run the program.

In other words, first you have to provide some way to run the script remotely; then you can run it.

Is there any way to access the script remotely right now? Can you use RSH or SSH to do it? Is there an application server you could use? Other options?
Gobind Singh
Ranch Hand

Joined: Aug 04, 2006
Posts: 62
I will be calling the script from a linux machine using SSH.
How to do that in Java?
Gobind Singh
Ranch Hand

Joined: Aug 04, 2006
Posts: 62
Basically this is what I am trying to do:

A script gets kicked off in the linux machine which calls a java progam.

The java program needs to do the following:

1.CREATE SOME DATA AND WRITE IT TO A FLAT FILE {myFile.txt) ON LOCAL MACHINE
2.FTP {myFile.txt} TO THE REMOTE MACHINE
3.INVOKE A SCRIPT on the remote machine that takes {myFile.txt} as an argument
4. Go into a while loop checking for the existence of a trigger file on the remote machine. (i.e the program blocks until the script has finished and created a trigger file)
5. when trigger file found, continue.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18824
    
  40

Originally posted by Gurps Bassi:
I will be calling the script from a linux machine using SSH.
How to do that in Java?


The same way that you execute any program locally. The difference is that the local program is "ssh", and the remote program is one of the parameters to "ssh". Like so...



One other issue, you may also want to disable passwords between the account from the localhost running your java program, and the user account on the remotehost. Otherwise, you will have to deal with the password prompt.

To get the instructions for this, google for "keygen ssh with password". You should also run the ssh command from the command prompt, for two reasons. First, to confirm that password has been disabled. Second, to say yes, when ssh prompts whether you should save the key.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Callling a Script from a java program