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)