JAVA: Runtime.getRuntime().exec() The system cannot find the file specified - java

I have an exe file: "TVRRun.exe" that is located in the directory: "C:\Program Files (x86)\TeleVantage\Client\Reporter".
I'm trying to run that exe file from java code:
public static void runTVRRun() throws IOException {
String path = System.getenv("programfiles(x86)") + "\\TeleVantage\\Client\\Reporter\\";
String[] command = {"TVRRun.exe", "-S", templatePath, "-S", tmpReportPath};
Runtime.getRuntime().exec(command, null, new File(path));
}
but I get the following error:
Exception in thread "main" java.io.IOException:
Cannot run program "TVRRun.exe" (in directory "C:\Program Files (x86)\TeleVantage\Client\Reporter"):
CreateProcess error=2, The system cannot find the file specified
I've tried all variations of the exec function, and also ProcessBuilder, but the same error appears.

Related

Backup mysql database from Java using mysqldump failed

I am using mysqldump like this:
Runtime.getRuntime().exec("mysqldump -u USERNAME -pPASSWORD DBNAME > /path/to/location/backup.sql");
in order to dump it into my local files, my java program is deployed using kubernetes.
Here is my code:
#RequestMapping(value = "/testDumping", method = {RequestMethod.POST, RequestMethod.GET})
public Object test(#RequestBody Map<String,Object> params) throws IOException {
String runStatement = (String)params.get("runStatement");
Runtime runtime = Runtime.getRuntime();
Process exec = runtime.exec(runStatement);
return exec;
}
And I finally got this exception "java.io.IOException: Cannot run program "mysqldump": error=2, No such file or directory". What is the problem here?
The error message is telling you exactly what the problem is:
java.io.IOException: Cannot run program "mysqldump": error=2, No such file or directory
This means that the shell is unable to find the executable file for "mysqldump". Try giving the entire absolute path to mysqldump inside your command like i.e. (assuming linux-like system):
Runtime.getRuntime().exec("/usr/bin/mysqldump -u USERNAME -p PASSWORD DBNAME > /path/to/location/backup.sql");
Exact path on Linux can usually be found using command which mysqldump
You could as well add this path to you PATH environment variable and keep your command as is (not 100% sure of this one as I'm usually working on Windows and am far from a Linux expert).

Java: No such file or directory when redirecting command line program output to /dev/null

I'm currently working on invoking bash program using java. The bash program output too much message and I want to redirect them to /dev/null. But I encountered a weird error No such file or directory.
Here is my demo.
public static void main(String[] args) {
try {
// Another version I've tried:
// Process p = Runtime.getRuntime().exec("echo a > /dev/null");
ProcessBuilder b = new ProcessBuilder("echo a");
// b.redirectOutput(new File("/dev/null")).redirectErrorStream(true);
b.redirectOutput(ProcessBuilder.Redirect.to(new File("/dev/null")))
.redirectErrorStream(true);
Process p = b.start();
p.waitFor();
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
}
And the error message as follows:
java.io.IOException: Cannot run program "echo a": 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 test.main(test.java:12)
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)
... 2 more
I'm using a MacBook with Catalina, and I tried java 1.8.0_231 and 1.8.0_241 from oracle. (I couldn't use higher java version because one of the dependency of my project requires java 8).
To ignore the output from the process, it's easier and more portable to use ProcessBuilder.Redirect.DISCARD than explicitly redirecting to a special file/device such as /dev/null.
b.redirectOutput(ProcessBuilder.Redirect.DISCARD)
.redirectErrorStream(true);
Forget about using Runtime.exec - that method is badly designed and hard to use safely. If you want to do input redirection with the "> /dev/null" style, you need to remember that > is a construct created by the command interpreter shell, not the operating system, and if you want to use it you must run a shell.
Runtime.getRuntime().exec(new String[] {"sh", "-c", "echo a > /dev/null"});

Running yarn job from java program using ProcessBuilder gives file does not exist error

I am trying to run a yarn job from a java wrapper program. The mapreduce jar takes two inputs:
A header file: I dont know the name of the file but the location and file extension and there's only one file at that location
A Input files directory
Apart from these I have an Output directory.
the processbuilder code looks like:
HEADER_PATH = INPUT_DIRECTORY+"/HEADER/*.tsv";
INPUT_FILES = INPUT_DIRECTORY+"/DATA/";
OUTPUT_DIRECTORY = OUTPUT_DIRECTORY+"/";
ProcessBuilder mapRProcessBuilder = new ProcessBuilder("yarn","jar",JAR_LOCATION,"-Dmapred.job.queue.name=name","-Dmapred.reduce.tasks=500",HEADER_PATH,INPUT_DIRECTORY,OUTPUT_DIRECTORY);
System.out.println(mapRProcessBuilder.command().toString());
Process mapRProcess = mapRProcessBuilder.start();
On run, I get the following error:
Exception in thread "main" java.io.FileNotFoundException: Requested
file /input/path/dir1/HEADER/*.tsv does not exist.
But when I run the same command as :
yarn jar jarfile.jar -Dmapred.job.queue.name=name -Dmapred.reduce.tasks=500 /input/path/dir1/HEADER/*.tsv /input/Dir /output/Dir/
It works all fine.
what can be the issue when running the command from java is causing this issue?
The * is being treated as part of the literal string in this case rather than a wildcard. Therefore globbing isn't expanding to your desired path name.
If there is only one file in the directory, why don't you find what its path is and pass that as the argument instead
eg.
File dir = new File(INPUT_DIRECTORY+"/HEADER);
if (dir.list().length > 0)
String HEADER_PATH = dir.list()[0].getAbsolutePath();

trouble with execute an command at apache exec in java

i want to run a file (a.txt) by using apache commons exec libraries . but it gives me this error : what is problem?
Exception in thread "main" java.io.IOException: Cannot run program "a.txt" (in directory "C:\Users\sinaa\Desktop"): CreateProcess error=2, The system cannot find the file specified
my code is :
public static void main(String[] args) throws IOException
{
Executor exec = new DefaultExecutor();
File temp=new File("C:\\Users\\sinaa\\Desktop");
exec.setWorkingDirectory(temp);
CommandLine s=new CommandLine("a.txt");
exec.execute(s);
}
From your comment:
i want to open a.txt in notepad window when i compile the code
To open up a File in Java using the default associated application, use the Desktop class:
try{
Desktop.open("a.txt");
}catch(IOException io){
io.printStackTrace();
}

Running external executable file from Java program

I'm trying to write a Java program which gets a executable file to run under Linux. This executable file receives two parameters and performs a nmap -sP operating with the two given parameters.
I've called this file as file.exe and its content is the one below:
nmap -sP $segment1-$segment1
I already did a chmod +x file.exe and it's in same directory where the .class is present.
The Java code is the following:
import java.lang.Runtime;
import java.lang.Process;
import java.io.IOException;
import java.lang.InterruptedException;
public class runFile {
public static void main (String args[]) throws IOException, InterruptedException {
Runtime r = Runtime.getRuntime();
Process p = r.exec("file.exe "+args[0]+" "+args[1]);
p.waitFor();
}
}
After compiling, Whenever I try to run it (from where the file.exe is) by
java runFile
I'm getting the following exception and errors log:
Exception in thread "main" java.io.IOException: Cannot run program
"file.exe": error=2, No such file or directory at
java.lang.ProcessBuilder.start(ProcessBuilder.java:1041) at
java.lang.Runtime.exec(Runtime.java:617) at
java.lang.Runtime.exec(Runtime.java:450) at
java.lang.Runtime.exec(Runtime.java:347) at
runFile.main(runFile.java:12) Caused by: java.io.IOException: error=2,
No such file or directory at java.lang.UNIXProcess.forkAndExec(Native
Method) at java.lang.UNIXProcess.(UNIXProcess.java:135) at
java.lang.ProcessImpl.start(ProcessImpl.java:130) at
java.lang.ProcessBuilder.start(ProcessBuilder.java:1022) ... 4 more
What am I missing?
The error is telling you that the executable can not be found in the current directory or the OS's search path.
Try including the Pathans part of the command
Process p = r.exec("/path/to/file.exe "+args[0]+" "+args[1]);
You should also consider separating each command/argument as a separate parameter
Process p = r.exec(new String[]{"/path/to/file.exe ", args[0], args[1]});
This will help with parameters that contain spaces.
You should also consider using ProcessBuilder, this will allow you to change the directory context that the command should be executed
Your java program looks for file.exe in the directory where you started your java program. It does not look inside the directory with your class files.

Categories

Resources