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:

All things are lawful, but not all things are profitable.

Campbell Ritchie

Marshal

Posts: 64171

215

posted 2 weeks ago

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.

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.

#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.

When it comes to destroying a civilization, gas chambers cannot hold a candle to echo chambers.

Lia Tas

Greenhorn

Posts: 12

posted 1 week ago

@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

posted 1 week ago

. . . but the streams associated with the Process take or send Strings.

Campbell Ritchie

Marshal

Posts: 64171

215

posted 1 week ago

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

posted 1 week ago

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

posted 1 week ago

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

posted 1 week ago

@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

posted 1 week ago

But do you give the name and password as part of the command:
$ <yourscript> some_user some_password