Results 1 to 4 of 4
- 03-07-2010, 11:10 AM #1
Member
- Join Date
- Feb 2010
- Posts
- 15
- Rep Power
- 0
calling a jar file from within java application
Hi,
I am trying to use daisydiff and it provides a jar file which I easily manage to run from cmd passing it parameters (2 file names). I need to call this jar file - passing it the parameters as well, from within my java application. Is it possible please? and if yes could you guide me how I should do it?
thanks
Roberta
- 03-07-2010, 03:16 PM #2
If the jar file that contains his utility with the main method is in the class path. from your code you should be able to use the reflection API to invoke the method. Get a handle on the class, and then the method, and invoke it with the string parameters that you would have used on the command line.
Java Code:String mainClassName="my.package.class.Name"; // set this. String[] args = new String[] {"param1", "param2" }; // and set the params here // Class<?> mainClass = null; try { mainClass = Class.forName(mainClassName); } catch (Exception ex) { throw new IllegalArgumentException("class not found in your jar file " + mainClassName); } // ok, class loaded, now find the main method Method mainMethod = null; try { mainMethod = mainClass.getMethod("main", String[].class); } catch (Exception ex) { throw new IllegalArgumentException("class to launch (" + mainClassName + ") does not have a public static void main(String[]) method"); } // and invoke main() mainMethod.invoke(null, (Object) args);
- 03-07-2010, 04:12 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,386
- Blog Entries
- 7
- Rep Power
- 17
- 03-10-2010, 08:11 PM #4
Member
- Join Date
- Feb 2010
- Posts
- 15
- Rep Power
- 0
Similar Threads
-
Calling a .Net webservice(Password Protected) from java console application
By charan reddy in forum Advanced JavaReplies: 2Last Post: 03-23-2010, 04:46 AM -
Calling Action class from web.xml(first page of application)
By ghdforjava in forum Advanced JavaReplies: 0Last Post: 08-10-2009, 01:25 PM -
excecuting a jar file by calling a java class
By Lavanya.vitria in forum Advanced JavaReplies: 1Last Post: 12-13-2008, 12:11 PM -
Batch file to Run Java application
By nitishlnt in forum EclipseReplies: 1Last Post: 10-24-2008, 07:46 PM -
calling a java application from another
By kratoras in forum New To JavaReplies: 1Last Post: 06-14-2008, 02:54 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks