• 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
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

Executing shell script in java

 
Greenhorn
Posts: 18
  • 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 :-)
 
Sheriff
Posts: 6193
164
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • 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:
 
Marshal
Posts: 65474
249
  • 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.
 
Sheriff
Posts: 21788
104
Eclipse IDE Spring VI Editor Chrome Java Ubuntu 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: 18
  • 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"
 
Bartender
Posts: 21004
128
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • 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: 18
  • 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: 65474
249
  • 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: 65474
249
  • 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: 18
  • 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 ...
 
Master Rancher
Posts: 4225
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: 18
  • 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: 4225
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: 18
  • 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: 4225
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: 65474
249
  • 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 an experimental device that will make my mind that most powerful force on earth! More powerful than this tiny ad!
Enterprise-grade Excel API for Java
https://products.aspose.com/cells/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!