• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to run shellscript code using java language  RSS feed

 
ashok movva
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone suggest me how to code in order to run a shell script code from a java application program...

Thanks in advance..
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What have you tried?
 
ashok movva
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, I don't have any idea about it.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Start by searching for Michael Daconta when Runtime.exec won't and read his article carefully. Also find out about the ProcessBuilder(←link) class.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ashok movva wrote:No, I don't have any idea about it.

the problem there is that we have no idea what kind of help you need. Do you need help installing the JDK? Can you write and compile a hello world program?  Do you have an existing application that already runs, but you want to add this as a feature?

etc.

Help us help you by giving us enough information to have a clue where to start.
 
Knute Snortum
Sheriff
Posts: 4274
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael Daconta's article is very thorough (and a little outdated), but since this post came from Beginning Java, I'm assuming that the OP doesn't need full production-ready code.  For most situations where you know the command that's being executed and you don't need to capture output, this code will do:

Still, it's good to know the shortcomings of that code.
 
div tripathi
Ranch Hand
Posts: 31
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are two methods that work generally well depending on what kind of flexibility you need within your code and for the script you need to call. Here are examples:
    String[] cmdScript = new String[]{"/bin/bash", "path/to/myScript.sh"};
    Process procScript = Runtime.getRuntime().exec(cmdScript);

If you only need to call your script without any other parameters, that is the most direct method. Although you can add parameters directly after that name of the script if needed, complex parameters (spaces, special characters, etc) can cause formatting issues and lead to bugs.

If you want to be able to debug easier and keep your parameters separate, you can use the ProcessBuilder class, something like:
    Process procBuildScript = new ProcessBuilder("path/to/myScript.sh", "myArg1 myArg2").start();
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!