• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

Executing shell script in java  RSS feed

 
Lia Tas
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi , I am new here and I hope that you can understand my problem

I have a shell script which is located in a folder. How can I search for the specific shell script which I want to execute ?

I think I should work with Runtime.exec(command) ..
And I am not sure how to add the path of the folder into the command string. ?

Thank you all :-)
 
Knute Snortum
Sheriff
Posts: 5931
155
Chrome Eclipse IDE Java Postgres Database Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch, Lia Tas!

You're right, you need to use Runtime.exec().  I'm not sure what the problem would be with using a path in your exec command string.

Here is one way to do this:
 
Campbell Ritchie
Marshal
Posts: 64171
215
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch again

Search for when Runtime.exec() won't by Michael Daconta, and you will find this. Be sure to read and understand that article, despite its age, then you will know why Knute was using exitValue() and waitFor(). It is probably simpler to use a ProcessBuilder object, which wasn't available when Daconta wrote, but the principles are the same.

Porcesses aren't a “beginning” topic, so I shall move you to a different forum.
 
Rob Spoor
Sheriff
Posts: 21719
102
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With both Runtime.exec and ProcessBuilder it's possible to specify the folder where you want to start your command. With Runtime.exec, it's one of the two overloaded methods that take a File as the final argument; the second argument can remain null. With ProcessBuilder it's with the directory method.
 
Lia Tas
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi thanks for your answer, there are a lot of scripts in the folder. The script should be a specific one. It has to be equal to string. Means that :

if(script.equals("test.sh"){
try{
Process process...)
}
}

And when I try this code I get the message" create process =5, access denied"
 
Tim Holloway
Saloon Keeper
Posts: 20655
122
Android Eclipse IDE Java Linux Redhat Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
#1 rule for Runtime.exec is never assume directories. Always use an absolute path for the command you're executing, and either explicitly change the working directory to be what you think it is anyway (or wherever you need it) as an absolute path, or use absolute file and directory names in your commands.

Actually, I'd say Rule #1 is avoid situations where you need Runtime.exec, but that's a different matter...

Also, in Unix-like OS's, when you execute a script, that script must have the "excutable" file attribute set for the class of user you are running as. That means, for example, if you are the non-owner of the script, it has to have 0xx5 permissions, if you're non-owner, but a member of the owner group, if must have 0x5x and so forth.

 
Lia Tas
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Knute Snortum

I tried it like your code with some changes and it works finde I just added  the Bufferreader for the Output. The problem is, that the output of my Shellscripte is not a string.. it is a password and username query.. It does not work with output string..
 
Campbell Ritchie
Marshal
Posts: 64171
215
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . but the streams associated with the Process take or send Strings.
 
Campbell Ritchie
Marshal
Posts: 64171
215
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, I may have been mistaken about the streams handling Strings. They seem to be InputStreams and OutputStreams. You would usually combine or redirect them with a method of ProcessBuilder's. You can read them with a buffered reader or a Scanner or similar.
 
Lia Tas
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried to give it as arguments through the process builder but this is still not working .. so the shell script is not executing because of the password and username ...
 
Dave Tolls
Master Rancher
Posts: 4042
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you run the script on the command line do you provide the username and password on the same line, or does the script prompt you for them?
 
Lia Tas
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Dave Tolls

So I have a shell script. If I want to execute this in command line not with a jar file or something like that I have to enter a password and a username.

This action I want to do with a jar file automatically... So I have to give the password and username to the process builder as arguments ? So I tried it but it does not work.. the password is test "user" is also "utest".. so executing the shell script is working but commit the password and username for executing it automatically via the jar file is still not working ☹️
 
Dave Tolls
Master Rancher
Posts: 4042
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But do you give the name and password as part of the command:

$ <yourscript> some_user some_password


Or does the script prompt for it:

$ <yourscript>
Please enter your username: some_user
Please enter your password: some_password

?

Finally, what does your code to execute and monitor the script currently look like?
 
Lia Tas
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I have a script like this:

test.sh
echo "hello login"
read user
read pw

And the Java code above ^^ when I execute this script nothing happens it's like frozen
 
Dave Tolls
Master Rancher
Posts: 4042
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So show the code.
Because that looks like you need to have your code interact with the script.

If you run the script like:
$ ./test.sh <username> <password>

Does it actually work?
If not, then that's why your Java code fails to execute it, because it sounds like that's what your Java code is trying to do.
 
Campbell Ritchie
Marshal
Posts: 64171
215
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please answer DT's question. I think the answer to his question is no.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!