3

I have 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's working good but when I trying changle variable "lol" files doesn't create in hard disk

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

What should I do ?

Thanks for your reply :)

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
kunkanwan
  • 191
  • 1
  • 2
  • 4
  • Haven't done a lot of Java on Linux, but possibly a permissions issue -- maybe the sandbox doesn't let you create files outside the home dir? Just a guess, maybe something to look into. – Kaleb Brasee Jun 13 '10 at 13:41
  • Thx for reply but I set chmod 777 and when I don't use getPath() file turn up. – kunkanwan Jun 13 '10 at 13:53
  • Note: `Runtime#exec()` doesn't throw any exception if the command failed. You'd like to read its output or error stream. Also see this link (all the 4 pages) http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html – BalusC Jun 13 '10 at 14:18

5 Answers5

5

Here is the solution to your problem . I faced a similar problem and this worked for me by specefying the output directory, it should execute the output of your files in that 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();
Asim Mushtaq
  • 445
  • 5
  • 14
1

You are probably using a java.io.File
In that case getPath() doesn't return the absolute path. For example:

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"

So, conclusion: use java.io.File.getAbsolutePath().

Tip: there also exists a java.io.File.getAbsoluteFile() method. This will return the absolute path when calling getPath().


I just read your comment to the other answer:

I think you did:

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

This won't work, because the os searches for an application called "touch /home/pc/example.txt".
Now, you are thinking "WTF? Why?"
Because the method Runtime.getRuntime().exec(String cmd); splits your string up on the spaces. And Runtime.getRuntime().exec(String[] cmdarray); doesn't split it up. So, you have to do it by yourself:

String[] cmd = {"touch", "/home/pc/example.txt"};
Runtime.getRuntime().exec(cmd);
Martijn Courteaux
  • 67,591
  • 47
  • 198
  • 287
0

like write code for real path

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

here u can get real path ..........

frictionlesspulley
  • 11,070
  • 14
  • 66
  • 115
sameer
  • 1
  • 3
0

Simply look at the content of lol, when you called x.getPath(). I would guess it is not an absolute path and the file gets created, but not where you expect it to be.

It x is a Java.io.File us getCanonicalPath() for an absolute path.

ZeissS
  • 11,867
  • 4
  • 35
  • 50
  • Good point but if I print x.getPath() gets result equals "/home/pc/example.txt". It's should be OK. When I using Runtime.getRuntime().exec("touch /home/pc/example.txt") it's work fine, but when I trying using function it's doesen't work. – kunkanwan Jun 13 '10 at 13:52
0

If the code works when you set the string to the literal "/home/pc/example.txt", and x.getPath also returns the same value, then it MUST work - it's as simple as that. Which means that x.getPath() is actually returning something else. Maybe there's whitespace in the string? Try comparing the strings directly:

if (!"/home/pc/example.txt".equals(x.getPath())) throw new RuntimeException();
Mike Baranczak
  • 8,291
  • 8
  • 47
  • 71