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
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.
Related
Recently, I have switched to Ubuntu v20.04 from Windows 10 Pro v2004 because of performance purposes. When, I was on Windows I can freely compile a java project from another java program by writing:
String pathToCompiler = "\"C:/Program Files/Java/jdk-14/bin/javac\"";
Process compileProcess = Runtime.getRuntime().exec(pathToCompiler+" -d bin #.sources", null, new File("ProjectPath"))
Where the sources file is a file containing the list of classes of the project
The code above works successfully on Windows 10.
But On Linux(Ubuntu):
if I substitute the value of variable pathToCompiler as
pathToCompiler = "\"/usr/lib/jvm/java-11-openjdk-amd64/bin/javac\""
the below exception is raised up and the program executing the command exits:
"/usr/lib/jvm/java-11-openjdk-amd64/bin/javac" -d bin #.sources
java.io.IOException: Cannot run program ""/usr/lib/jvm/java-11-openjdk-amd64/bin/javac"" (in directory "/home/arham/Documents/Omega Projects/Project0"): error=2, No such file or directory
at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1128)
at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1071)
at java.base/java.lang.Runtime.exec(Runtime.java:592)
at java.base/java.lang.Runtime.exec(Runtime.java:416)
at ide.utils.systems.BuildView.lambda$3(BuildView.java:267)
at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.io.IOException: error=2, No such file or directory
at java.base/java.lang.ProcessImpl.forkAndExec(Native Method)
at java.base/java.lang.ProcessImpl.<init>(ProcessImpl.java:340)
at java.base/java.lang.ProcessImpl.start(ProcessImpl.java:271)
at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1107)
The problem is that the file actually exists but it says No Such File or Directory
Actually, The program which is compiling the project is a Java IDE that I am creatiing.
Someone please tell if he/she knows how to fix this bug
The Runtime.exec method has several problems that make it difficult to use, and this is one of them. Use the newer ProcessBuilder class instead.
String pathToCompiler = "C:/Program Files/Java/jdk-14/bin/javac";
Process compileProcess = new ProcessBuilder(pathToCompiler, "-d", "bin", "#.sources")
.directory(new File("ProjectPath"))
.start();
The differences are:
Remove the extra quotes from around the path to the executable. If quoting is needed, the system takes care of it.
Pass the each command line arguments as a separate string. This way you don't have to worry about quoting.
Update the path to the following:
String pathToCompiler = "/usr/lib/jvm/java-11-openjdk-amd64/bin/javac/";
I am trying to move files (in linux OS) by using java code this happen when trying to move single file. Issue which looks to me is i guess similar to
java.nio.Files.move() - DirectoryNotEmptyException on OS X
If so how can we handle it . Shall it be done on linux level or java.
Files.move(src, dst, new CopyOption[] { StandardCopyOption.REPLACE_EXISTING });
in this src folder is present in projs/dd/output/TAR
and dest folder is config_files/billing/xml/workArea
the error which comes is even though destination there is no file present nor folder in target:
WARNING: FAILED_TO$Failed to get MHS user handler
java.nio.file.DirectoryNotEmptyException:
config_files/billing/xml/workArea/GBF_2017030001_000007540_00001_0000000004
at sun.nio.fs.UnixCopyFile.move(UnixCopyFile.java:491)
at sun.nio.fs.UnixFileSystemProvider.move(UnixFileSystemProvider.java:262)
at java.nio.file.Files.move(Files.java:1347)
what i could see is both folders are on different FileSystem
by running stat -c "%d" "${Folder}" on linux OS.
is this cause of exception
I'm currently working on a program to zip and unzip files/directories and it behaves kind of weird when I call it over the git bash.
The program takes three arguments (zip/unzip, inputPath, outputPath).
For Example:
java -jar zip_unzip.jar --zip H:\\zip_test H:\\test5\zip_test_4.zip
Everything works fine when I call it over Eclipse or CMD. It creates the directory structure if it doesn't already exist and zips the input Folder into the newly created output folder. But when I call it over the git bash like in the example above it somehow "ignores" the backslash and instead of creating a folder called test5\, creates a zip-archive called test5zip_test_4.zip
Here's a snippet of the code that takes care of creating the directory structure, where zippedFolder is the outputPath-Parameter:
File directoryToZip = new File(inputFolder);
String targetZippedFolder = zippedFolder;
targetZippedFolder = targetZippedFolder.replace("\\", "/");
//create directory to store archive in, if it doesn't already exist
File destDir = new File(targetZippedFolder.substring(0, targetZippedFolder.lastIndexOf("/")));
if (!destDir.exists()) {
destDir.mkdirs();
}
In line 3 of the codesnippet I'm replacing every backslash with a forwardslash because I thought this would make it platform-independent.
Can someone explain the behavior of the git bash to me and maybe suggest a more platform-independent path handling method?
I built a project in eclipse that uses a txt file. the file is located in the main folder project.
I get the file name as argument via command line, and I get FileNotFoundException. I try to use Scanner Object and get the file name as input from eclipse.. and it's worked. (I insert only the file name : file.txt . not the full path)
so why via the eclipse it's work and with command line not?
thank you!
this is the exception:
java.io.FileNotFoundException: bigMaze.txt (The system cannot find the file spec
ified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:146)
at java.io.FileReader.<init>(FileReader.java:72)
at BFS.BFS.readFile(BFS.java:43)
at BFS.BFS.InsertMaze(BFS.java:57)
at BFS.BFS.StartMain(BFS.java:16)
at search.main(search.java:20)
Exception in thread "main" java.lang.NullPointerException
at BFS.BFS.InsertMaze(BFS.java:62)
at BFS.BFS.StartMain(BFS.java:16)
at search.main(search.java:20)
If you're running from the command line, try placing the file in the same directory as the .class file
ProjectRoot
bin
file.txt
program.class
src
If the program is running from eclispe, then the file should go where you originally had it. directly under the project root.
This is all considering your running the program with String filename = "file.txt";
I think the problem is regarding pathname of your txt file. In case of command prompt you have to provide full path like: "MyComputer://D/yourFile.txt" but using eclipse you can give just "D://yourFile.txt". It will work.
Why does this code cause an error: access denied?
public void armazenaPerfil() throws FileNotFoundException, IOException {
FileOutputStream out = new FileOutputStream(this.login + "_perfil.mbk");
ObjectOutputStream objOut = new ObjectOutputStream(out);
objOut.writeObject(this);
System.out.println("Escrevi!");
objOut.close();
}
The error message:
ric93_perfil.mbk(acess denied)
java.io.FileNotFoundException
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(FileOutputStream.java:212)
at java.io.FileOutputStream.<init>(FileOutputStream.java:104)
at br.uefs.ecomp.myBook.model.Perfil.armazenaPerfil(Unknown Source)
Access denied problems are basically the operating system saying "You are not allowed to write that". Basically, an OS-level access control / permissions issue is preventing you from reading or writing the file at the specified location.
When you write a file using a relative pathname, the JVM will attempt to write it in a location relative to the running application's current working directory. What directory that will be depends on how the JVM is launched, but if you launch from a command prompt using the java command, it will be the command shell's current directory.
You can find out what the current director actually is using the one-liner suggested by Brendan Long:
System.out.println(new File(pathname).getAbsolutePath());
where pathname is the pathname of the file you were trying to read or write. Note that this doesn't actually check that the pathname refers to an existing file, or tell you that you should be able to create or open the file. It merely tells you what the absolute pathname for the file would be.