Calling EXE programs with Java code (including parameter passing)

Source: Internet
Author: User

Using Java code to invoke EXE1 usage scenarios

I am now using the ECLIPSE+TOMCAT architecture to build a Web server, in this Web project I need to set up a timed task to execute a local C # project generated EXE in a timed task.

2Java Code

public class MyTask extends TimerTask {public  void run () {     System.out.println ("call at" + (new Date ()));    Todoæ¤å¤?æ» Å? å... Ľ "Ä»»å?¡ä»£ç      try {         String cmdstr1=" d:/download/svmgridparametertraining/svmgridparametertraining/ Easysvmtrain/bin/debug/easysvmtrain newtrain.txt newtest.txt ";                          Runtime.getruntime (). EXEC ("cmd.exe/c start" +cmdstr1);     catch (Exception e) {         e.printstacktrace ();}}  }


GetRuntime (). The parameters in exec () include the EXE file full path and parameters (the two txt parameters behind me are written directly in the back)


/C means to close the command line after execution.


3 Traps: exe work path


After executing the code above, the Discovery program does not perform as expected. Tests have found that newtrain.txt and newtest.txt as parameters are empty. so I analyzed the program's execution path and did not have these two files .


Add the following code to the calling C # program:

Headache 2014/8/29 13:28:11 Console.WriteLine ("Environment.currentdirectory:" + environment.currentdirectory); Console.WriteLine ("Directory.GetCurrentDirectory ():" + directory.getcurrentdirectory ()); String input2 = Console.ReadLine ();

Print out the working directory of the project, which can be found. When you invoke a C # project separately, the working directory is a C # Engineering catalog, and when you use Tomcat +eclipse to do timed tasks, the printed directory is the Tomcat directory.


Because the source program needs to use the Newtrain and newtest files are in the C # directory, so through Java calls simultaneous go into the Newtrain and newtest these two parameters is actually meaningless (Tomcat does not have these two files)


4 Solution

The parameter file is placed in the appropriate directory according to the current working directory that is printed.

Calling EXE programs with Java code (including parameter passing)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.