Question

This is a very specific problem and I wasn't able to find any single clue anywhere. So, I'm using Runtime.getRuntime().exec() in order to call many different Linux commands from my Java program. The following code works fine with almost any type of Linux command when I apply slight changes (using bash and string array or not).

command = "ls -l | grep something";
strArr1 = new String[]{"/bin/sh" , "-c", command};
p1 = Runtime.getRuntime().exec(strArr1);
exitVal = p1.waitFor();

However, my program hangs and does not finish processing when I use the following command:

command = "more file0.txt";

I also, tried different variations for example the following one gives error:

command = "more keywords/file0.txt";
strArr1 = new String[]{command};
p1 = Runtime.getRuntime().exec(strArr1);
exitVal = p1.waitFor();

or

command = "more keywords/file0.txt";
p1 = Runtime.getRuntime().exec(command);
exitVal = p1.waitFor();

error is:

There was a problem:java.io.IOException: Cannot run program "more keywords/file0.txt": error=2, No such file or directory

Indeed, file0.txt exists.

So, I was wondering if Linux command more works in Runtime.getRuntime().exec()? I really need this command works because it produces results for other commands. Thanks in advance.

Was it helpful?

Solution

Your program hangs because more command being executed in shell expects user to hit Enter/whitespace to continue scrolling. Same happening when it is executed from Java. You may want to try cat instead, if you need output to stdout.

OTHER TIPS

Yes, you should not use more, as many others have indicated, but the error you are having is probably because you are doing a more on a file that doesn't exist... at least not in the place where more is running.

Try to put the full location of your file and the error should go away.

For example, if the file is located in the root temp folder, do a

more /temp/keywords/file0.txt

If you don't know what is the location of the file, pwd should tell you where the file is. Go to the folder of the file file0.txt and do a pwd there.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top