0

I need to launch an external app called TeamViewer from my JavaFX application. So I've TeamViewer.app file which I'm copying to a temporary folder and launching it using:

Runtime.getRuntime().exec("open /path/to/Teamviewer.app");

But this is throwing Directory not empty IOException.
I also tried launching using shell file where I wrote "open /path/to/Teamviewer.app" command to launch.sh and launched launch.sh using process created by ProcessBuilder.
if I run launch.sh from terminal, it works. But from java program, following exception is thrown:

SEVERE: null
java.io.IOException: Cannot run program "sh" (in directory "/Applications/ColorDx.app/Contents/Java"): error=66, Directory not empty
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:1047)
    at com.sc.colordx.controller.ColorDxSupportController.executeCommand(ColorDxSupportController.java:288)
    at com.sc.colordx.controller.ColorDxSupportController.launchSetup(ColorDxSupportController.java:126)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)

What could be the reason for directory not empty exception? It has to be non empty as I've copied TeamViewer.app there.
Could this be a multi threading issue? Means I'm copying TeamViewer.app first and immediately launching it. Is there a chance that launch command is called before copying is finished?
TIA

Ruturaj Patil
  • 608
  • 1
  • 10
  • 25
  • i would first try to fire the java .exec(...) in a folder that contains a simple app (without that copy part first). Also i would try with `Process proc=Runtime.getRuntime().exec(new String[]{"sh","simpleapp"}));` Does that work? – chris May 27 '15 at 06:27
  • no it doesn't work even without copying. In fact instead of copying the teamviewer app through Java I copied using shell command itself. Means before "open simpleapp" in launch.sh I wrote cp command to copy it first. But still got same error that directory not empty. – Ruturaj Patil May 27 '15 at 07:22
  • So i would suggest to fix the problem 'run unix process from java'. May this helps: http://stackoverflow.com/questions/525212/how-to-run-unix-shell-script-from-java-code – chris May 27 '15 at 07:28
  • Thanks but Commons exec eventually uses same thing I think. I'd tried that earlier. didn't work. Perhaps there's something wrong with environment variables. – Ruturaj Patil May 27 '15 at 11:48
  • See also [When Runtime.exec() won't](http://www.javaworld.com/article/2071275/core-java/when-runtime-exec---won-t.html) for many good tips on creating and handling a process correctly. Then ignore it refers to `exec` and use a `ProcessBuilder` to create the process. – Andrew Thompson May 28 '15 at 12:26
  • Thanks. I tried everything! including GoodWindowsExec example almost as it is from above link. There's something terribly wrong with that project runtime. Because I just ran 'ls' command instead of 'sh' without any params and still it says 'No such file or directory' !!! Any clue? – Ruturaj Patil May 28 '15 at 15:08

1 Answers1

0

As I'd suspected, there was a problem with runtime itself! There's one more JavaFX app that acts as installer for this app (app with Runtime problems). For installation of this app I had used following code to copy contents of .app file:

public static final void copyFile( File source, File destination ) throws IOException {


                    FileChannel sourceChannel = new FileInputStream( source ).getChannel();
                    FileChannel targetChannel = new FileOutputStream( destination ).getChannel();
                    sourceChannel.transferTo(0, sourceChannel.size(), targetChannel);
                    sourceChannel.close();
                    targetChannel.close();
        }


Above code didn't preserve file attributes while copying. Replacing above method with Files.copy using COPY_ATTRIBUTES option solved the problem.

Ruturaj Patil
  • 608
  • 1
  • 10
  • 25