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();
}
Related
I have the following code to run three executions :
public static void main(String[] args) throws InterruptedException, IOException
{
String filepath1 = "cmd /c gradlew jmhJar";
String filepath2 = "cmd /c java -jar path/to/the/file/filename.jar -rf csv -rff path/to/save/file1.csv -wi 3 -i 5 -f 2";
String filepath4 = "cmd /c javac path/to/the/file/ParserHash.java";/*Code to compile is parserHash.java*/
String filepath3 = "cmd /c java path/to/the/compiled/class/ParserHash "C:/Users/msek/Desktop/trial/result/file1.csv C:/Users/msek/Desktop/trial/result/file2.csv C:/Users/msek/Desktop/trial/result/file3.csv";
try
{
runProcess(filepath1);
runProcess(filepath2);
System.out.println("Sucessfully written into file1.csv");
runProcess(filepath4);
System.out.println("Compilation Over");
runProcess(filepath3);
System.out.println("Program Sucessfully Executed");
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static void runProcess(String processString)
{
try
{
final Process p = Runtime.getRuntime().exec(processString);
new Thread(new Runnable() {
public void run() {
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = null;
try {
while ((line = input.readLine()) != null)
System.out.println(line);
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
p.waitFor();
}
catch (Exception x)
{
x.printStackTrace();
}
}
If I compile the java file going to that particular directory and compile its compiling successfully and on running it, it executes successfully. But if I pass it like "cmd /c path/to/java/file/file.java" its getting compiled but when I execute it, I get an error stating that could not find or load mainclass eventhough class file is present.
I have looked various links on this which suggested buid process, but that didn't work.
I just want to know where I'm going wrong and how to compile, execute a java file by passing multiple arguments using Runtime.exec()..
java path/to/the/compiled/class/ParserHash
If you're having trouble with an exec() you should:
Try the command from a command line yourself. It will fail the same way in this case.
Look up the syntax of the command. In this case you will learn that the argument to the java command is not a path but a class name, fully qualified, i.e. including the package name. With dots.
I want to run :
export JAVA_HOME=/home/sofiane/install/jdk1.8.0_121/
export ANDROID_HOME=/home/sofiane/Android/Sdk/
npm run android
from a program java using commande line.
Thank you.
Java class
public static void executeCommands() throws IOException {
ProcessBuilder pb = new ProcessBuilder("bash", "./shell.sh");
pb.inheritIO();
Process process = pb.start();
try {
process.waitFor();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
shell.sh
#!/bin/bash
export JAVA_HOME=/home/sofiane/install/jdk1.8.0_121/
export ANDROID_HOME=/home/sofiane/Android/Sdk/
cd /home/sofiane/work/MyAcceleo/fichier/pfe
npm run android
Result :
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());
}
}
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
I am trying to convert an HTML file to pdf and view it using my pdf viewer(vsmartpdf.exe).Its a cmd command which goes like "vmartpdf.exe -c 'path of html file' 'path of output folder' ". I am trying to execute this command using java program . Below is what i did.
import java.io.IOException;
public class LoadTesting implements Runnable {
#Override
public void run() {
try {
//String command = "C:\\Users\\vishalt\\Desktop\\New Source\\deliver\\vsmartpdf\\vsmartpdf.exe";
//Runtime.getRuntime().exec("cmd /c "+command);
//Process process = new ProcessBuilder("cmd.exe", "/c", "cd \"C:\\Users\\vishalt\\Vsmartfinal\" && dir").start();
Runtime rt = Runtime.getRuntime();
String[] cmd = { "C:\\Users\\Desktop\\Vsmartfinal\\vsmartpdf.exe", "-c", "C:\\Users\\vishalt\\Desktop\\output\\SCB_MOLPU.HTML", " C:\\Users\\vishalt\\Desktop\\output\\"};
Process p = rt.exec(cmd);
System.out.println("Called");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
then i am calling this thread . But i am getting error as
CreateProcess error=2, The system cannot find the file specified.
Can somebody please help me with it
The error message means that C:\Users\Desktop\Vsmartfinal\vsmartpdf.exe doesn't exist at the time when the code is executed.
A common source for this problem is that this executable exists in a developer machine but not on the production server.