Reputation: 173
I am working on building a web application that needs to use a standalone JAR file at its core. Essentially, I have an application module packaged as a JAR file that I can run successfully on the Command prompt using a command like: java -jar myapp.jar
and can pass some additional parameters as needed (basically the file name to process). This JAR has some dependencies on other JAR files, which are placed in the libs folder located in the same folder as the myapp.jar
file. All this is working fine as a standalone JAR file executed through the command prompt.
Now, I need to use this JAR inside a web application and do the same kind of processing. I have already got a Grails based web application setup with the required UI, etc. and I have added all the required JAR libs inside the lib
folder of my Grails web application. I have placed myapp.jar
file as well in the same lib folder.
I am a little confused about how to run this myapp.jar
file through my web application's Controller methods? Basically, I have a form in my web application in which the users would specify the required file path needed to be processed by myapp.jar
and then user would click the Run/Submit button. Upon that I need to launch myapp.jar
with the supplied parameters of the file path and put the resulting/processed file at some specified location.
So, essentially do the processing similar to the standalone JAR execution on command prompt through the Web application's controller method.
Can someone kindly guide me regarding the right process to accomplish this?
Any help in this regard would be greatly appreciated.
Upvotes: 2
Views: 502
Reputation: 11035
If you look at the MANIFEST.MF
in the /META-INF/
directory within myapp.jar
file you should find the Main-Class
attribute. If, for example, the main class is com.example.MyApp
you should be able to call it's static main method from your code (i.e., from your Grails Controller method).
String[] args = new String[] { '/path/inputFile', '/path/outputFile' }
com.example.MyApp.main(args)
// do something with output file
If you have access to the source of myapp.jar
you might be better off reviewing what main
does and then just use the API directly.
Upvotes: 3