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();
}
Related
I am trying to find out whether a command exists (eg. date) using the command shell builtin, on Ubuntu. However the following (scroll further below for java snippet)
//main.kt
fun main(){
val proc=ProcessBuilder("command","-v","date").start() //line 37
}
fails to run with stack trace
Exception in thread "main" java.io.IOException: Cannot run program "command": error=2, No such file or directory
at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1143)
at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1073)
at MainKt.main(main.kt:37)
at MainKt.main(main.kt)
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:314)
at java.base/java.lang.ProcessImpl.start(ProcessImpl.java:244)
at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1110)
... 3 more
command is definitely available on my bash -
user#pc:~$ type command
command is a shell builtin
Why does the error occur?
Note that most other similar queries (there are plenty on SO) got solved via syntactic corrections or bad file paths, and don't apply here.
Env:
JDK 17 on Ubuntu 20.04.5 LTS
Java code:
import java.io.IOException;
//rough.java
public class rough {
public static void main(String[] args) throws IOException {
new ProcessBuilder("command","-v","date").start();
}
}
Your first sentence already has the solution:
[…] using the command shell builtin […]
ProcessBuilder execs processes directly and does not invoke a shell. And since a shell-builtin is a functionality provided by the shell and not a binary, it cannot be invoked (directly) with ProcessBuilder.
If you want to run a shell, you need to do so explicitly:
new ProcessBuilder("sh", "-c", "command -v date").start();
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"});
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.
I cant seem to get file IO to work within netbeans
public static void main(String[] args) throws IOException
{
System.out.println("File Location: "+ System.getProperty("myFile.txt"));
//File file = new File("myFile.txt");
}
I get an exception for this.
Exception in thread "main" java.lang.NullPointerException
And output:
File Location: null
without the System.getProperty and I get the FileNotFoundException
java.io.FileNotFoundException: myFile.txt (The system cannot find the file specified)
The file itself is in the source directory of the project and also shows up in the IDE under the Source Packages > myproject> myFile.txt
Any reason why its not finding the file or any suggestions?
Thanks
File should be placed here if you're calling it like this "myFile.txt"
MyProject (project root)
myFile.txt
src
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.