How to run jhipster command from a java program - java

I'm trying to run the jhipster import-jdl command from a java program as show below.
public boolean generate(String foldername, String filename) {
Runtime rt = Runtime.getRuntime();
try {
IPath root = ResourcesPlugin.getWorkspace().getRoot().getLocation();
Process proc = rt.exec(new String[] { "cmd", "/K",
"cd " + root + "\\" + foldername + " && jhipster import-jdl " + filename });
}
catch (IOException e) {
e.printStackTrace();
}
return true;
}
But the entity files are not getting created.
But if I open the command prompt in the folder specified in the code and run the same command as shown below manually, the entity files are generated.
cd C:\GitClone\work && jhipster import-jdl swaggertest.jh
I'm new to these things and have no idea on where am I going wrong. Please help me with the same.

Related

Using gcc directly with ProcessBuilder creates no output files

I'm trying to compile C source file with gcc using ProcessBuilder. I managed to make it running but there is no output file.
Here is my code (based on this answer) :
public void compileWithGccNoCmd(Path sourcePath) throws IOException {
String sourcePathString = sourcePath.toString();
String outputPathString = sourcePath.getParent().toString() + "\\" + Files.getNameWithoutExtension(sourcePath.toString());
try {
ProcessBuilder pb = new ProcessBuilder("gcc", "-fprofile-arcs -ftest-coverage -o " + outputPathString, sourcePathString);
pb.directory(sourcePath.getParent().toFile()); // this was added later
Process compile = pb.start();
compile.waitFor();
if (compile.exitValue() == -1) {
// if error
System.out.print("COMPILE ERROR");
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
My first thought that it has something to do with the working directory so I add pb.directory() but the output file still not there.
Since I can't find any solution to this, I tried another way by running gcc with cmd. I set the working directory to make sure the output files on the right place. This is the alternative code (based on this answer):
public void compileWithGcc(Path sourcePath) throws IOException {
String sourcePathString = sourcePath.toString();
String outputPathString = sourcePath.getParent().toString() + "\\" + Files.getNameWithoutExtension(sourcePath.toString());
ProcessBuilder pb;
try {
pb = new ProcessBuilder("cmd", "/C", "gcc " + sourcePathString + " -o " + outputPathString + " -fprofile-arcs -ftest-coverage");
pb.directory(sourcePath.getParent().toFile());
Process p = pb.start();
p.waitFor();
int x = p.exitValue();
} catch (Exception ex) {
ex.printStackTrace();
}
}
For some reason it works! Why is this happened? I'm pretty sure both is running gcc with the same parameter
This line:
ProcessBuilder pb = new ProcessBuilder("gcc", "-fprofile-arcs -ftest-coverage -o " + outputPathString, sourcePathString)
needs to look like this:
ProcessBuilder pb = new ProcessBuilder("gcc", "-fprofile-arcs", "-ftest-coverage", "-o", outputPathString, sourcePathString)
What you are doing with the first line is passing two arguments to gcc, one of which is "-fprofile-arcs -ftest-coverage -o " + outputPathString, instead of passing the five arguments of my edited line. gcc won't understand that.
When you use cmd like you did, it parses the command that you gave it and passes the arguments correctly, that's why it works.

Trying to run a command-line driven C++ Project in Java

I have a c++ program (.exe) that runs in the command line. You don't open it with cmd, it just opens the command prompt on its own and uses it as a menu. I also have a login system built in Java. My issue is, I'm trying to open my C++ program in the Java program, and it just isn't working. Here is the latest iteration of the relevant code:
btnLaunchMyProgram.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
File file1 = new File("C://Users//" + System.getProperty("user.name") + "//Desktop//My Program//myprogram.exe");
if(!file1.exists()) {
try {
HttpDownloadUtility.downloadFile("https://www.example.com/downloads/MyProgram.exe", "C://Users//" + System.getProperty("user.name") + "//Desktop//My Program//");
} catch (IOException e) {
e.printStackTrace();
}
}
try {
Process p = Runtime.getRuntime().exec(new String[] {"cmd.exe", "\"" + "C://Users//" + System.getProperty("user.name") + "//Desktop//My Program//MyProgram.exe" + "\""});
} catch (IOException e) {
e.printStackTrace();
}
}
});
In order to get rid of a few concerns you might have
- I have also tried the following, which runs my program in the background, but doesn't open the command prompt:
Process p = Runtime.getRuntime().exec(new String[] {"C://Users//" + System.getProperty("user.name") + "//Desktop//My Program//MyProgram.exe"});
and also:
Process p = Runtime.getRuntime().exec(new String[] {"cmd.exe", "C://Users//" + System.getProperty("user.name") + "//Desktop//My Program//MyProgram.exe"});
None of these work.
Just to be clear about what I need to happen:
1. I press the button (btnLaunchMyProgram)
2. It opens the exe and, by extension, the command prompt / console.
3. That's basically all it needs to do.

-javaagent: option recognised as a command

In an application I am writing, I need to use the javaagent option to call an external jar in the same folder as my current jar file. When I run the code from the jar file, I get told: "Error: Could not find or load main class -javaagent:" but when I am running it from a batch file, it works as excepted.
I am using a ProcessBuilder to start the application:
String java = System.getProperty("java.home") + File.separatorChar + "bin" + File.separatorChar +"java.exe";
File transagent = new File(pluginDir + File.separatorChar + "TransAgent.jar");
String doublequote = String.valueOf('"');
List<String> commandlist = new ArrayList<String>();
commandlist.add(java);
commandlist.add(" -javaagent:");
commandlist.add(doublequote);
commandlist.add(transagent.getAbsolutePath());
commandlist.add(doublequote);
for(int i = 0; i < commandlist.size(); i++){
String part = commandlist.get(i);
System.out.print(part);
}
System.out.println();
ProcessBuilder pb = new ProcessBuilder();
pb.command(commandlist);
pb.redirectError(Redirect.appendTo(errorfile));
pb.redirectOutput(Redirect.appendTo(logfile));
try {
pb.start();
} catch (IOException e) {
e.printStackTrace();
}
But, when I go to the error file, I see "Error: Could not find or load main class -javaagent:"
This would usually be thrown if the option isn't valid, but I've checked the dash to work file. And I put what printed from the application in a batch file, and it worked fine. Why?
You can try this code below:
ProcessBuilder pb = new ProcessBuilder("java", "-javaagent:"+transagent.getAbsolutePath(), "YouMainClass");
pb.redirectError(Redirect.appendTo(errorfile));
pb.redirectOutput(Redirect.appendTo(logfile));
try
{
pb.start();
}
catch(IOException e)
{
e.printStackTrace();
}

How to handle spaces in path to a windows batch file?

I have a batch file on windows machine.
The path to the same is having spaces in it. E.g. C:\Hello World\MyFile.bat
I am trying to execute the batch file through java as:
Runtime.getRuntime().exec(dosCommand + destinationFilePath + batch)
But, as the path has spaces, it says "C:\Hello" is not a valid command or directory.
I tried this too:
Complete command: cmd /c start /wait "C:/Hello World/MyFile.bat" It opens the command prompt, but does not go to the folder Hello World and does not execute the bat file
How do I handle this situation.
Let me know if any additional Info. is required.
Using quotation marks ("C:\Hello World\MyFile.bat") should do the trick. Within Java you'll have to secape the quotation marks with \ (String batch = "\"C:\Hello World\MyFile.bat\"").
I was able to solve it using ProcessBuilder.
The directory in which the bat file is present can be added to the working directory as:
processBuilder.directory(new File("C:\hello world\"));
This works like gem.
int result = 1;
final File batchFile = new File("C:\\hello world\\MyFile.bat");
final File outputFile = new File(String.format("C:\\hello world\\output_%tY%<tm%<td_%<tH%<tM%<tS.txt", System.currentTimeMillis()));
final ProcessBuilder processBuilder = new ProcessBuilder(batchFile.getAbsolutePath());
processBuilder.redirectErrorStream(true);
processBuilder.redirectOutput(outputFile);
processBuilder.directory(new File("C:\\hello world\\"));
try {
final Process process = processBuilder.start();
if (process.waitFor() == 0) {
result = 0;
}
System.out.println("Processed finished with status: " + result);
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
Did you try to escape quotes surrounding the path such as :
Runtime.getRuntime().exec(dosCommand + "\"" + destinationFilePath + batch + "\"")
I just solved this problem using a ProcessBuilder as well, however I gave the directory with a space to processBuilder.directory and ran the command with the bat file name.
ProcessBuilder pb = new ProcessBuilder("cmd", "/c", "start", "/wait", "export.bat");
pb.directory(new File(batDirectoryWithSpace));
pb.redirectError();
try {
Process process = pb.start();
System.out.println("Exited with " + process.waitFor());
}
catch (IOException | InterruptedException ex) {
Exceptions.printStackTrace(ex);
}

Open a cmd window and issue multiple commands

I have to open (or execute in the background) a cmd window out of my java application. It did this before and it worked totally fine:
public String getEnvVar(String envVarName) throws Exception {
String varName = "%" + envVarName + "%";
Process process = Runtime.getRuntime().exec(
new String[]{"cmd.exe", "/C", "echo " + varName}
);
BufferedReader input = new BufferedReader(
new InputStreamReader(process.getInputStream())
);
varName = input.readLine();
process.destroy();
...
My problem is: I have a different instance of a different class and tried to open a cmd window just the way it is done in the code above:
private void execute() {
try {
String batchData = batchContent();
Process process = Runtime.getRuntime().exec(new String[]{"cmd.exe", "/C", batchData});
new File(sourceFile).delete();
new File(sourceFolder).delete();
process.destroy();
} catch (IOException ie) {
}
}
But this doesn't work...I used google to figure out why, couldn't find a real reason for this.
Even
Process process = Runtime.getRuntime().exec(new String[]{"cmd.exe", "cd c:\\temp"});
does not change into the temp folder (which i created beforehand for testing reasons)
What's wrong?
EDIT//
I now tried:
private void upload() {
try {
//Process process = Runtime.getRuntime().exec(batchDataFile);
//Process process = Runtime.getRuntime().exec("cmd.exe /C " + batchDataFile);
Process process = Runtime.getRuntime().exec("cmd.exe /C " + "\"" + batchDataFile + "\"");
//new File(sourceCodeFile).delete();
//new File(batchDataFile).delete();
//new File(sourceCodeFolder).delete();
process.destroy();
} catch (IOException ie) {
ie.printStackTrace();
}
}
I additionally tried to run the generated batch skript without the Java app, it works as intended

Categories