2

I try to execute a shell command via java like this

if (Program.isPlatformLinux())
{
    exec = "/bin/bash -c xdg-open \"" + file.getAbsolutePath() + "\"";
    exec2 = "xdg-open \"" + file.getAbsolutePath() + "\"";
    System.out.println(exec);
}
else
{
    //other code
}
Runtime.getRuntime().exec(exec);
Runtime.getRuntime().exec(exec2);

but nothing happens at all. When I execute this code it prints /bin/bash -c xdg-open "/home/user/Desktop/file.txt" in the console, but does not open the file. I have also tried to call the bash first and then the xdg-open-command, but there is not change.

What's the problem here and how can I solve this?

EDIT: The output of the calling looks like this:

xdg-open "/home/user/Desktop/files/einf in a- und b/allg fil/ref.txt" xdg-open: unexpected argument 'in'

But this seeems very strange to me - why is the command seperatet before the in even the entire path is set in quotation marks?

zimmerrol
  • 4,872
  • 3
  • 22
  • 41
  • 1
    Try checking if there is some error by printing the process output as [shown in this question](http://stackoverflow.com/questions/3936023/printing-runtime-exec-outputstream-to-console). Maybe you get a more specific error to work on. – BackSlash Nov 12 '16 at 23:51
  • @BackSlash I followed your advice and added the results from this to my original post. – zimmerrol Nov 13 '16 at 00:03
  • *"einf in a- und b"* is this a folder name containing spaces? – Timothy Truckle Nov 13 '16 at 00:44
  • See [this question](http://stackoverflow.com/questions/31776546/why-does-runtime-execstring-work-for-some-but-not-all-commands) for why `Runtime.exec(String)` fails when the command contains quotes. – that other guy Nov 13 '16 at 03:18

1 Answers1

2

Please note that you don't need xdg-open to do this. You can use the java platform-agnostic Desktop API:

if(Desktop.isDesktopSupported()) {
    Desktop.open("/path/to/file.txt");
}

Update

If the standard approach still gives issues, you can pass the parameters as an array since Runtime.exec does not invoke a shell and therefore does not support or allow quoting or escaping:

String program;
if (Program.isPlatformLinux())
{
    program = "xdg-open";
} else {
    program = "something else";
}

Runtime.getRuntime().exec(new String[]{program, file.getAbsolutePath()});
that other guy
  • 116,971
  • 11
  • 170
  • 194
BackSlash
  • 21,927
  • 22
  • 96
  • 136
  • This was my first approch. But it does not seem to work: `xdg-open/home/user/Desktop/files/einf\ in\ a-\ und\ b/allg\ fil/ref.txt xdg-open: unexpected argument 'in\'` – zimmerrol Nov 13 '16 at 00:06
  • You need to escape the backslash. Check the answer again – BackSlash Nov 13 '16 at 00:06
  • I cannot use the `Desktop` class as it seems to crash the entire application at some devices (see http://stackoverflow.com/questions/40554515/java-swing-desktop-open-causes-gtk-error) – zimmerrol Nov 13 '16 at 00:06
  • Your code is also not working, with the same error as above. – zimmerrol Nov 13 '16 at 00:11
  • Alright, this seem to be working. So you have an idea how to fix the real problem, that the Desktop class is crashing the application on some devices? – zimmerrol Nov 13 '16 at 00:46