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.
Related
I'm trying to run an Ubuntu image from a java program using a script; here is how:
my java code:
public static void main(String[] args) {
executeCommand("/home/abrahem/IdeaProjects/untitled3/src/createContainer.sh");
}
public static void executeCommand(String filePath) {
File file = new File(filePath);
if (!file.isFile()) {
throw new IllegalArgumentException("The file " + filePath + " does not exist");
}
try {
if (isLinux()) {
Process p = Runtime.getRuntime().exec("sh " + filePath);
p.waitFor(); // i tried to remove this but still not work for my me
} else if (isWindows()) {
Runtime.getRuntime().exec("cmd /c start " + filePath);
}
} catch (InterruptedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
here is my createContainer.sh script file:
#!bin/sh
sudo docker run ubuntu
when I go to bin and type:
docker ps
or
docker ps -a
It should show the running Ubuntu container, but it doesn't.
Note: there is nothing wrong with the shell location; I try to create file in shell file and it works.
You do not capture any error messages or normal output from your process. Maybe it just works?
Use getErrorStream() and getOutputStream() methods of Process to capture the output from the process somewhat like described here. You may just see the expected output. If not, it should be the error message on the error stream.
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 :
Please know that stanford is not exe. it is a folder consists many programs
I open the cmd.exe by using following statement:
public static void runStanfordCMD() throws IOException{
List<String> cmds = Arrays.asList("cmd.exe", "/C", "start", "java", "-mx4g", "-cp", "*", "edu.stanford.nlp.pipeline.StanfordCoreNLPServer");
ProcessBuilder builder = new ProcessBuilder(cmds);
builder.directory(new File("D:/Desktop/stanford-corenlp-full-2015-12-09"));
Process proc = builder.start();
}
so how to close the cmd.exe after I finished some process?
by using ProcessBuilder or Runtime?
If using ProcessBuilder how to write the statement according to my case?
how to write the statement to the Runtime according to my case?
public static void closeStanfordCMD() throws IOException{
try {
Runtime.getRuntime().exec("command.exe /C" + "Your command"); // how to write the statement?
} catch (Exception e) {
e.printStackTrace();
}
}
The method
Runtime.getRuntime().exec
returns an object of Process.
Process has a method destroy() which kills the process you're running.
So what you need is to store the Process object returned by the exec method and call destroy() on it when you want to kill the process.
Also you can call waitFor() method to wait for the process to stop (Causes the current thread to wait, if necessary, until the process represented by this Process object has terminated).
To make it clear, try this code (or modify for your needs):
try {
Process p = Runtime.getRuntime().exec("java -mx4g -cp * D:/Desktop/stanford-corenlp-full-2015-12-09/edu.stanford.nlp.pipeline.StanfordCoreNLPServer");
p.waitFor();
}
catch (IOException e) {
e.printStackTrace();
}
catch (InterruptedException e) {
e.printStackTrace();
}
// how to write the statement?
if you want to close command prompt:
Runtime.getRuntime().exec("taskkill /IM " + "cmd.exe");
if you want to close stanford.exe:
Runtime.getRuntime().exec("taskkill /IM " + "stanford.exe");
I'm trying to run a simple command from console using this
public void execute(File file, String... command){
Process p = null;
ProcessBuilder builder = new ProcessBuilder("ls");
builder.directory(file.getAbsoluteFile());
builder.redirectErrorStream(true);
try {
p = builder.start();
} catch (IOException e) {
LOGGER.error("", e);
}
}
but it kept saying that I cant run ls, permission denied. Is there any missing step here?
Thanks
You should use pass the commands and the flags to the constructor of the ProcessBuilder separately (as per the docs):
public void execute(File file, String... command) {
ProcessBuilder builder = new ProcessBuilder("ls", "-l");
builder.directory(file.getAbsoluteFile());
builder.redirectErrorStream(true);
try {
Process p = builder.start();
} catch (IOException e) {
LOGGER.error("", e);
}
}
It seems you want to execute command, though. To do this, you can pass command to ProcessBuilder's constructor.
public void execute(File file, String... command) {
ProcessBuilder builder = new ProcessBuilder(command);
builder.directory(file.getAbsoluteFile());
builder.redirectErrorStream(true);
try {
Process p = builder.start();
} catch (IOException e) {
LOGGER.error("", e);
}
}
Here's the ideone to the working code
You'll notice that when I run it with "ls -l", there's a problem executing the command. This is because the first argument is treated as the command to be executed and the remaining arguments are treated as flags.
To change permissions of bash commands in EC2 instances, execute
chmod u+x /home/admin/ec2-api-tools-*/bin/*
This depends on a couple of things:
1) The user that runs java (your process will be ran as that user)
2) The directory where your JAR or class resides.
Also make sure that your account has proper permissions if the java user is not the same as the user you are logged in as.
ProcessBuilder builder = new ProcessBuilder("ls -l");
There is no process named "ls -l". You want to use the process named "ls" with the arguments "-l", for that you need:
ProcessBuilder builder = new ProcessBuilder("ls", "-l");
I am trying to run reg files with Java. I tried this with no luck:
import java.awt.Desktop;
import java.io.File;
import java.io.IOException;
public class RegEdit {
public static void main(String[] args) throws IOException {
// Desktop.getDesktop().open(new File("ihindi.reg"));
String[] cmd = {"regedit", "ihindi.reg"};
Process p = Runtime.getRuntime().exec(cmd);
try {
p.waitFor();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
ihindi.reg
Windows Registry Editor Version 5.00
[HKEY_CURRENT_USER\Software\Policies\Microsoft\Internet Explorer\Control Panel]
"HomePage"=dword:00000001
When I run it, it doesn't make anything and errors. Where am I doing wrong ?
I think you would need to add the "/s" statement in between, your process probably got disturb while you're writing the data into the regedit.
I was in the exactly same situation as yours, no error, but it just couldn't write into the regedit. the "/s" did the job.
try{
// silence all the process without prompting the dialog box to ask if user wanna proceed.
String[] cmd = { "regedit.exe", "/s", regFilePath};
Process process = Runtime.getRuntime().exec(cmd);
process.waitFor();
}catch (InterruptedException e){
System.out.println(e);
}
There are all sorts of problems with this. The following line:
String[] cmd = {"regedit", "ihindi.reg"};
should pass the full path to the ihindi.reg file, not just the file name.
Also,
It is possible that a dialog box is preventing that waitFor() call from ever returning.
You should call regedit with the /s switch to silence those dialog boxes.
Also, you might consider using a ProcessBuilder like so:
import java.awt.Desktop;
import java.io.File;
import java.io.IOException;
public class RegEdit {
public static void main(String[] args) throws IOException {
// Desktop.getDesktop().open(new File("ihindi.reg"));
//you will need to figure this out
String ihindiPath = getIhindiPath();
ProcessBuilder processBuilder = new ProcessBuilder("regedit", "/s", ihindiPath)
try {
processBuilder.start().waitFor();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
I think the problem is your paths, with your current code *.reg would have to be in the same directory as the jar file. You can however set the working directory explictly when usong ProcessBuilder:
ProcessBuilder pb = new ProcessBuilder("regedit", "myreg.reg");
pb.directory("c:/");//thus our file should be located in c:\myreg.reg
Process p = pb.start();
This can achieved through Process Builder in JAVA. Please consider the following example for this:
ProcessBuilder processBuilder = new ProcessBuilder("regedit", "reg_file_to_run.reg");
Process processToExecute = processBuilder.start();
And then you can optionally wait for the completion of process execution with this line:
processToExecute.waitFor();
Note: If command in your registry file asks for confirmation prompts while making changes in registry entries, you can perform it silently as well with '/s' option. Like this:
ProcessBuilder processBuilder = new ProcessBuilder("regedit", "/s", "reg_file_to_run.reg");
Withthis command would be executed silently without any confirmation prompt.