• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Tim Cooke
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
Saloon Keepers:
  • Piet Souris
Bartenders:

Callling a Script from a java program

 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does anyone know how I can call a bash shell script from a java program?
 
author and iconoclast
Posts: 24208
47
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check out the "exec()" family of methods in the java.lang.Runtime class.
 
Gobind Singh
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Gobind Singh
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, How do I execute a script on a remote machine using my java program?
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24208
47
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I will be calling the script from a linux machine using SSH.
How to do that in Java?
 
Gobind Singh
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author
Posts: 23965
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic