java, execute exe from WAR in java code - java

In my war, I have file exe in WEB-INF\classes\
How can I execute this file in Java code (How can I specify path to this file) ?
command = " ? ";
Process x = p.exec(command);

Te following approach could work:
1) Prepare full path of your executable:
ServletContext context = getContext();
String fullPath = context.getRealPath("/WEB-INF/classes/executable");
2) Execute like you would normally do it:
String[] cmd = { fullPath /*[...] arguments */};
Process p = Runtime.getRuntime().exec(cmd);
p.waitFor();
This is a simplified example; you may also want to read more about ProcessBuilder.

This is bad idea. Imagine simply fact that your .war packege should run on almost any server (".war is platform independend") and your .exe file is compiled just for one architecture.
Better should be execute your .exe as external program just for separate platform independent and platform dependent part. Then in java you can test operating system and on this basis run desired externel programm.
Read this link with similar question.

The best way to do find a file's real location inside a web app is to use the ServletContext.getRealPath (see http://docs.oracle.com/javaee/6/api/javax/servlet/ServletContext.html#getRealPath(java.lang.String))
You can access that object from the session...

Related

Java Spring Boot failing to find Python Script in resources folder

I am trying to call python Scripts(in resources) from my Java class.
This is my code spinnet
String res = "/Scripts/Brokers/__init__.py";
URL pathUrl = this.getClass().getResource(res);
String path = "";
if(pathUrl != null)
path = pathUrl.toString();
ProcessBuilder pb = new ProcessBuilder("/usr/bin/python3.6", path);
ProcessBuilder is giving error No such file or directory.
P.S.
value of path = "file:/home/path-to-project/project-name/out/production/resources/Scripts/Brokers/\__init__.py"
Also how to include python scripts in jar file to run the project on linux server.
I am stuck with this issue since last 3 days. Please suggest.
From java doc:
https://docs.oracle.com/javase/7/docs/api/java/lang/ProcessBuilder.html
Starting a new process which uses the default working directory and
environment is easy:
Process p = new ProcessBuilder("myCommand", "myArg").start();
So basically, you going to fork a sub process that will
start a python interpreter
with the python script that you have provided as argument.
The path to your script should be a normal path that your OS can understand, therefore you should not give a URI? like path (protocol:address/path).
if (path.contains(":"))
path = (path.split(":"))[1];
Also the backslash \ before __init__.py looks suspicious.
You should be able to run ls /home/path-to-project/project-name/out/production/resources/Scripts/Brokers/__init__.py and see the file, the same goes for ls /usr/bin/python3.6.

Open a file on Openshift using Java

I have a DIY cartridge. My project structure looks like
MY_PROJECT
-diy
-myProgram.jar
-resources
-file1, file2...
-.openshift
-action_hooks
-start
The myProgram.jar uses files from the folder 'resources'.
The code looks like
File imageFolder = new File("resources");
System.out.println("Image Folder read:"+imageFolder.canRead()); //canRead returns false
File[] listOfFiles = imageFolder.listFiles(); // here I get null
The program runs by action hook 'start':
nohup java -jar $OPENSHIFT_REPO_DIR/diy/*.jar --server.port=${OPENSHIFT_DIY_PORT} --server.address=${OPENSHIFT_DIY_IP} &
The problem is that I'm not able to work with files.
As described in code comments I get null on listFiles().
If I run the program on Openshift manually(ssh to server/$OPENSHIFT_REPO_DIR/diy/ and run java -jar ...) it works, but it doesn't work via action_hooks.
Thank you in advance!
I resolved the issue with Openshift env variable 'OPENSHIFT_REPO_DIR'.
Instead of using relative path
new File("resources");
I use absolute
String absolutePath = System.getenv("OPENSHIFT_REPO_DIR");
new File(absolutePath + "diy/resources")

Giving command line arguments to java program over git-bash

I'm currently working on a program to zip and unzip files/directories and it behaves kind of weird when I call it over the git bash.
The program takes three arguments (zip/unzip, inputPath, outputPath).
For Example:
java -jar zip_unzip.jar --zip H:\\zip_test H:\\test5\zip_test_4.zip
Everything works fine when I call it over Eclipse or CMD. It creates the directory structure if it doesn't already exist and zips the input Folder into the newly created output folder. But when I call it over the git bash like in the example above it somehow "ignores" the backslash and instead of creating a folder called test5\, creates a zip-archive called test5zip_test_4.zip
Here's a snippet of the code that takes care of creating the directory structure, where zippedFolder is the outputPath-Parameter:
File directoryToZip = new File(inputFolder);
String targetZippedFolder = zippedFolder;
targetZippedFolder = targetZippedFolder.replace("\\", "/");
//create directory to store archive in, if it doesn't already exist
File destDir = new File(targetZippedFolder.substring(0, targetZippedFolder.lastIndexOf("/")));
if (!destDir.exists()) {
destDir.mkdirs();
}
In line 3 of the codesnippet I'm replacing every backslash with a forwardslash because I thought this would make it platform-independent.
Can someone explain the behavior of the git bash to me and maybe suggest a more platform-independent path handling method?

VB.Net How to run the application in a specific folder?

So a little bit of my code is to run an application (a batch file in an external folder that the user can select.) The problem is that when I use
Path.GetDirectoryName(O.FileName) + "\BasicStart.bat"
however when I try to do the above it fails to open to java application due to the class folders trying to be referenced from where the vb.net application is launched
The code in the .bat is
#echo off
#title JavaApplet12
set CLASSPATH=.;dist\*
java -client -Dnet.sf.odinms.wzpath=wz server.Start
pause
And I know I can hard set the classpath and wzpath but users of my application will not want to do this.
How can I launch it in vb.net so that the application is launched from that location and can read the files from that folder location.
This should be resolvable by setting the working directory of the process before you start it:
Dim processStartInfo = New ProcessStartInfo()
processStartInfo.WorkingDirectory = Path.GetDirectoryName(O.FileName)
processStartInfo.FileName = "BasicStart.bat"
Process.Start(processStartInfo)

exec not working with java 1.7.21, but in netbeans works fine

I made a little program and it worked fine, but now. First, it mux the xml chapter file in the mkv file, so we get a muxed mkv file. Some day ago I updated java to 1.7.21 and I think this is the problem why it is not working now. It's a little strange, but when I run in netbeans everything is fine, but when I build and I run the .jar file, it is not working. It create the xml file, but not mux in the mkv file (and because not muxed not delete the xml file). Here is the code: (filename=xml file path; mkv=mkv file path)
public void muxing() {
try {
Runtime rt = Runtime.getRuntime();
Process p = rt.exec("c:\\Program Files\\MKVtoolnix\\mkvpropedit.exe --chapters \""+filename+"\" \""+mkv+"\"");
if (p.waitFor()==0) {
File xmlfile=new File(filename);
xmlfile.delete();
}
}
catch(Exception e) {
System.out.println(e.getMessage());
}
}
The program worked with java 1.6 and I think with 1.7.17 too. Win7 32bit. Sorry for my bad English.
Oracle has made breaking changes to Runtime.exec() in Java 7 update 21 (and 6 update 45).
If the program name contains spaces, you need to specify command and arguments in an array:
Process p = Runtime.getRuntime().exec(new String[] {
"C:\\Program Files\\MKVtoolnix\\mkvpropedit.exe",
"--chapters", "\""+filename+"\"", "\""+mkv+"\""});
Another option is to use java.lang.ProcessBuilder:
Process p = new ProcessBuilder("C:\\Program Files\\MKVtoolnix\\mkvpropedit.exe",
"--chapters", "\""+filename+"\"", "\""+mkv+"\"").start();
As stated by Oracle:
Applications that need to launch programs with spaces in the program name should consider using the variants of Runtime.exec that allow the command and arguments to be specified in an array.
Alternatively, the preferred way to create operating systems processes since JDK 5.0 is using java.lang.ProcessBuilder. The ProcessBuilder class has a much more complete API for setting the environment, working directory and redirecting streams for the process.

Categories

Resources