use >nul when execute command by runtime.exec - java

I am executing a self-defined command like following code.I know that append a >nul to command will prevent from outputing error or other message to console.But in my code,>nul is treated as a parameter of my own command deletefile.So the command can not execute as expected.How to resolve it?
Runtime runtime = Runtime.getRuntime();
try {
Process p = runtime.exec("deletefile /name:\"ABC\" /owner:\"Wolfman\" >nul");
int status = p.waitFor();
System.out.println(status);
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
Solution is:
cmd deletefile /name:\"ABC\" /owner:\"Wolfman\" >nul

Related

Linux is not executing command by Java

I am trying to run some servers from a bot, but I can't get it run my screen and host the server. I've tried debugging and getting some kind of code to debug, but I come up with nothing. Maybe it's something in my syntax?
These are the value you can run in: name = factorio, path = /opt/factorio/bin/x64/factorio, args = --start-server map.zip
Together makes:
screen -dmS factorio -m bash -c "/opt/factorio/bin/x64/factorio --start-server map.zip"
public boolean run() {
try {
System.out.println("screen -dmS "+serverName+" -m bash -c \""+path+" "+args+"\"");
Process proc = rt.exec("screen -dmS "+serverName+" -m bash -c \""+path+" "+args+"\"");
BufferedReader read = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
String str = null;
while ( (str = read.readLine()) != null)
System.out.println(str);
try {
proc.waitFor();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Exit value: "+proc.exitValue());
this.setStatus(ONLINE);
return true;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
}

executing a few command lines from java

I want to execute those few lines from java code but I didn't know how.
cd C:\\apps\\dcm4che-2.0.28\\dcm4che-tool\\dcm4che-tool-txt2dcmsr\\src\\bin
txt2dcmsr.bat -c C:\\apps\\dcm4che-2.0.28\\dcm4che-tool\\dcm4che-tool-txt2dcmsr\\src\\etc\\txt2dcmsr.cfg C:\\Users\\intidhar\\Desktop\\input\\tst.txt \"C:\\Users\\intidhar\\Desktop\\output\\tst.dcm
I wrote those lines of java code but it didn't work:
try {
Runtime runtime = Runtime.getRuntime();
runtime.exec("C:\\apps\\dcm4che-2.0.28\\dcm4che-tool\\dcm4che-tool-txt2dcmsr\\src\\bin\\txt2dcmsr.bat -c \"C:\\apps\\dcm4che-2.0.28\\dcm4che-tool\\dcm4che-tool-txt2dcmsr\\src\\etc\\txt2dcmsr.cfg\" \"C:\\Users\\intidhar\\Desktop\\input\\tst.txt\" \"C:\\Users\\intidhar\\Desktop\\output\\tst.dcm\"");
} catch (IOException e) {
e.printStackTrace();
}

Not able to execute command from java

I want to execute below command from java program .
java -jar jarfile.jar -dir C:/MyDirectory -out C:/MyDirectory/example.html
I tried following , but its opening cmd prompt , but it not executing next command
Runtime rt = Runtime.getRuntime();
try {
rt.exec(new String[] { "cmd.exe", "/c", "start" , "java -jar exampleJar.jar -dir C:\\MyDirectory -out C:\\MyDirectory\\exampleHtml.html" });
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
I am using the following piece of code and its working perfect,ProcessBuilder is used to create operating system processes and each instance manages a collection of process attributes. The start() method creates a new Process instance with those attributes. To create new subprocesses with the same instance start() method can be called repeatedly.
public void execute(String[] code){
try{
ProcessBuilder pb=new ProcessBuilder(code);
pb.redirectErrorStream(true);
Process process = pb.start();
BufferedReader inStreamReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
while(inStreamReader.readLine() != null){
System.out.println(inStreamReader.readLine());
}
} catch (IOException e) {
System.out.println(e.toString());
e.printStackTrace();
log.error(e.getMessage());
}
}

How to execute Symlink command in java

I am finding trouble in executing symlink command in java. My source filename and destination filename has space in it.
How to execute this command?
Eg. ln -sf /home/Desktop/image1 .jpg /home/Desktop/Folder/image 2.jpg
I am trying this code
String command = "ln -sf " + "/home/Desktop/\"image 1.jpg\"" + " /home/Desktop/Folder/\"image 2.jpg\"";
CommandLine oCmdLine = CommandLine.parse(command);
DefaultExecutor oDefaultExecutor = new DefaultExecutor();
oDefaultExecutor.setExitValue(0);
try {
oDefaultExecutor.execute(oCmdLine);
} catch (ExecuteException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
You can do it easy with NIO.2
Path directoryTarget = Paths.get("c:/temp");
Path directoryLink = Paths.get("c:/links/linkTemp");
Files.exists(directoryTarget);
try {
Files.createSymbolicLink(directoryLink, directoryTarget);
} catch (IOException e) {
e.printStackTrace();
}

Running external program

What's the difference between running programs using java and run it using the command line? In the first case it does not work, but in the second case it works fine.
Java:
try {
Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec("../../../my/prog \"//10.124.12.15/C:/output/*\" ../../../input/345 -N -A");
DataInputStream bis = new DataInputStream(proc.getInputStream());
int _byte;
while ((_byte = bis.read()) != -1)
System.out.print((char)_byte);
proc.waitFor();
} catch (IOException ex) {
ex.printStackTrace();
} catch (InterruptedException ex) {
ex.printStackTrace();
}
AND command:
../../../my/prog "//10.124.12.15/C:/output/*" ../../../input/345 -N -A
Try using absolute path. Maybe that's your problem.
Thanks all, I solved my problem:
try {
String cmd="/progs/my/prog //10.124.12.15/C:/output/* /temp/input/345 -N -A"
Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec(
new String[]{"/usr/bin/bash", "-c", cmd, "1>/dev/null 2>&1"});
proc.waitFor();
} catch (IOException ex) {
ex.printStackTrace();
} catch (InterruptedException ex) {
ex.printStackTrace();
}

Categories