Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Executing bash script with tilde in path

I am trying to execute in linux:

command[0] = "~/test/bin/runScript_sh";
Runtime.getRuntime().exec(command);

But get an exception java.io.IOException: Cannot run program
error=2, No such file or directory

Probably because it can not evaluate tilde.

What can be done?

like image 575
yuris Avatar asked Jul 23 '26 21:07

yuris


2 Answers

I would replace it myself.

if(path.s.substring(0,1).contains("~"))
    path = path.replaceFirst("~",System.getProperty("user.home"));

Which gets you the string you want.

like image 80
Tuim Avatar answered Jul 26 '26 11:07

Tuim


You can get the user's home directory with System.getProperty:

command[0] = System.getProperty("user.home") + "/test/bin/runScript_sh";
like image 26
Emil Vikström Avatar answered Jul 26 '26 11:07

Emil Vikström