Java - Runtime.getRuntime (). Exec () What is happening?

I have a problem with Runtime.exec () in Java. My code:

String lol = "/home/pc/example.txt";
String[] b = {"touch", lol}; 
try {  
    Runtime.getRuntime().exec(b);  
} catch(Exception ex) {  
    doSomething(ex);  
}

It works well, but when I try to change the variables "lol" files are not created on the hard drive

for example: String lol = x.getPath();where getPath () returns a String

What should I do?

Thanks for your reply:)

+3
source share
5 answers

Here is the solution to your problem. I ran into a similar problem, and it worked for me, having described the output directory, it should output your files in this working directory.

   ProcessBuilder proc = new ProcessBuilder("<YOUR_DIRECTORY_PATH>" + "abc.exe"); // <your executable path> 
   proc.redirectOutput(ProcessBuilder.Redirect.INHERIT);  // 
   proc.directory(fi); // fi = your output directory
   proc.start();
+5
source

, java.io.File
getPath() . :

System.out.println(System.getProperty("user.dir")); // Prints "/home/pc/"
// This means that all files with an relative path will be located in "/home/pc/"
File file = new File("example.txt");
// Now the file, we are pointing to is: "/home/pc/example.txt"
System.out.println(file.getPath()); // Prints "example.txt"
System.out.println(file.getAbsolutePath()); // Prints "/home/pc/example.txt"

, : java.io.File.getAbsolutePath().

: java.io.File.getAbsoluteFile(). getPath().


:

, :

String[] cmd = {"touch /home/pc/example.txt"};
Runtime.getRuntime().exec(cmd);

, os "touch /home/pc/example.txt".
: "WTF? ?"
Runtime.getRuntime().exec(String cmd); . Runtime.getRuntime().exec(String[] cmdarray); . , :

String[] cmd = {"touch", "/home/pc/example.txt"};
Runtime.getRuntime().exec(cmd);
+1

LOL, x.getPath(). , , , , .

It x Java.io.File us getCanonicalPath() .

0

, "/home/pc/example.txt", x.getPath , - . , x.getPath() - . , ? :

if (!"/home/pc/example.txt".equals(x.getPath())) throw new RuntimeException();
0

String path = request.getSession().getServletContext().getRealPath("/");

u ..........

0

Source: https://habr.com/ru/post/1749810/


All Articles