How to run a C++ exe file from Java? - java

I want to open an EXE file from a Java program. I tried 2 procedures.
The program can run some programs, like NotePad++, but cannot run my C++ EXE file. I tried:
Process exec = Runtime.getRuntime().exec(file.getAbsolutePath());
ProcessBuilder processBuilder = new ProcessBuilder(file.getAbsolutePath());
but neither of the above work. No exception is thrown, and exec.isAlive = true.

Your mistake is that you took the absolute path in the first procedure.
Try using a relative path, I just tested and it worked just fine.
This also works for me (using 2 backslashs').
Runtime.getRuntime().exec("C:\\Program Files\\DDNet\\DDNet.exe");
Kind regards

Related

Java run Python script Issue

ProcessBuilder pb = new ProcessBuilder("C:\\xxxxxxx\\python.exe", "C:\\xxxxxxxxxx\\1.py");
pb.redirectOutput(ProcessBuilder.Redirect.INHERIT);
Process p = pb.start();
I am trying to run Python script from my Java program. The problem is, java run doesn't give me any result. In Python script, there is an OCR operation and I am writing a txt file.
CMD - Manually - It runs OK
Python IDLE - Manually - It runs OK
.bat doesn't work. with administrative rights doesn't work.
Java run doesn't work.
I need help, I need to run the script from java program.
I solved the problem.
The problem is;
ProcessBuilder pb = new ProcessBuilder("C:\\xxxxxxx\\python.exe", "C:\\xxxxxxxxxx\\1.py");
pb.redirectOutput(ProcessBuilder.Redirect.INHERIT);
Process p = pb.start();
When you run this code, python script will process operations in that directory, I mean in your Java class' directory. No matter where py file is.
In my case, there were 'image.save("x.png")' lines in the python script. I hoped that images will be saved in the directory where the py file is. But it is not like that.

Shell script java program not writing files correctly

I have a java program that syncs the content of two directories. The program takes the two directories as arguments.
I created a shell script to run the program on Linux. When the program firsts runs it creates a .sync file in each directory, this work perfectly on windows but is causing me some issues on linux. I'm very new to linux so unsure what is really going on.
My program uses the following piece of code to create the .sync file...
FileWriter fileDir1 = new FileWriter(dir1 + "\\.sync");
fileDir1.write(obj.toJSONString());
fileDir1.flush();
fileDir1.close();
When I run the program on linux the files are being created in the same directory as the two directories are located instead of inside the two directories. The resulting files look like this..
dir1\.sync and dir2\.sync
Any help would be great.
Use a forward slash / instead of an escaped backslash \\, it works both on Windows and Linux.
FileWriter fileDir1 = new FileWriter(dir1 + "/.sync");

C# program wont be executed from Java

I have this C# program i made and while i can run it fine by clicking the exe file or by clicking on a batch file, I cant start up the program on a java program I made to run it. I have tried this line of code and couldn't get the software to run.
Runtime.getRuntime().exec("nameOfTheExeFile");
or set it to the batch file i made that starts the program.
Runtime.getRuntime().exec("nameOfTheBatchFile");
Now the interesting thing is when I try it with the batch file i get an error saying that the file cannot be found but when i double click the batch file it will start the exe file just fine.
I have even tried to use Process but I am not getting any luck with that process as well
List cmdAndArgs = Arrays.asList(new String[]{"cmd.exe", "/c", "ProgramName.exe"});
ProcessBuilder pb = new ProcessBuilder(cmdAndArgs);
Process p = pb.start();
Strange thing is i dont get any error at all. Even when i try unit testing i don't any error's at all. Is there a process I am missing or something ? I am lost on what to do.
Update:
When i check on the task manager i can see that the program is running but not the exe version. I see ProgramName.vshost.exe , is there a reason for this to be showing and not the exe file ?
Since your program is command line program you need to start it from cmd. I'm not sure if this is the best way to do it, but it works.
Runtime.getRuntime().exec("cmd /c start nameOfTheBatchFile");
Batch file:
start cmd.exe /k "nameOfExeFile"
exit

Run jar file from java application [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Run Java program into another Program
i try to run jar file from java application which was created in eclipse.
when i run jar file using below source code then fire Unable to access jarfile error
Process process = run.exec("java -jar TestJava.jar");
InputStream inError = process.getErrorStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inError));
System.out.println("Error=" + bufferedReader.readLine());
Sounds like TestJava.jar is in a different directory. If you're running this code within Eclipse, then the present working directory is going to be the same as your Eclipse project's folder (unless you've configured it differently, this is Eclipse's default run location). Either specify a path to TestJava.jar via an absolute path or a path relative to the present working directory.
One other thing you'll need to be mindful of - you need to consume both the error stream and the output stream of the Process you're creating. The default output/error stream buffer sizes of Process instances are not very big and, if full, will cause that Process to block indefinitely for more buffer space. I recommend consuming each stream in a separate Thread.
Set the working directory where this process should run. Set the working directory using the below statement.
Runtime.getRuntime().exec(command, null, new File("path_to_directory"));
First try the java -jar command on cmd window and see if you can run TestJava.jar. Then try running the same command from your code.
Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec("java -jar TestJava.jar");
System.exit(0);
You need to supply the full path to the Java command.
For example, under windows you'd need something like
C:/Program Files/java/jre/bin/java.exe
ProcessBuilder & Runtime.exec don't take the system path into account when trying to execute your command

Elevate permission to run exe in java

I need to run a bat file using a java code. I did that in the following way
Process process =Runtime.getRuntime().exec("cmd /c start D:\\Work\\BOSync\\TestFoxPro\\ATSFill.bat");
int exitVal = process.waitFor();
Problem is I can run the bat but the task of the bat not happened. I run the bat to load data from CSV file to oracle database using sqlldr. When I double click on the bat it works fine.
I think the problem is JVM doesn't has enough permission to run the bat. Is there a way to elevate the permission in java?
This sounds like a path issue to me. Try using absolute paths to the binary that you are using in your bat file and set other environment variables that your script need.
As for the cmd window popping up - try just calling the bat file directly and not use the cmd /c command.
Hey guys finally i Sort it out. The problem was in my bat file. It was like that previously. cd \C:\oracle\ora92\bin sqlldr GAMINI/gamini C:\AOTITS\CLSTMAS.ctl log=C:\AOTITS\CLSTMAS.log. Then I remove the path of oracle bin and add it to system path. Then it works fine. Thanks for your help

Categories