2012-03-11 18:58 阅读 93

试图在Java中复制PHP exec()函数

I'm trying to replicate PHP's exec function in a Java application. Here's the PHP code:


And here is the Java code:


When I run that I am getting an IOException saying:

Exception in thread "main" java.io.IOException: Cannot run program "C:/executer.au3": CreateProcess error=193, %1 is not a valid Win32 application
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:1029)
    at java.lang.Runtime.exec(Runtime.java:615)
    at java.lang.Runtime.exec(Runtime.java:448)
    at java.lang.Runtime.exec(Runtime.java:345)
    at Executer.execute_captcha_check(Executer.java:248)
    at _ProjectAmsterdam.main(_ProjectAmsterdam.java:36)
Caused by: java.io.IOException: CreateProcess error=193, %1 is not a valid Win32 application
    at java.lang.ProcessImpl.create(Native Method)
    at java.lang.ProcessImpl.<init>(ProcessImpl.java:189)
    at java.lang.ProcessImpl.start(ProcessImpl.java:133)
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:1021)
    ... 5 more

I'm a bit out of my element, how can I get this au3 file to open in Java?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • 已采纳
    drqefnd544707688 drqefnd544707688 2012-03-11 19:04

    The file you mention is an autoscript file that does not do anything by itself. Like a text file.

    You probably need to supply the program that will run the file - the Autoit.exe program for example.

    Runtime.getRuntime().exec("/Path/To/Autoit.exe C:/executer.au3");

    Along those lines - I cannot test at the moment.

    The PHP exec version may somehow trigger the default program for .au3 files. That's just a guess.

    点赞 评论 复制链接分享
  • dongxue9997 dongxue9997 2012-03-11 21:57

    Although running AutoIt.exe with the script as a command line argument works, and is probably fine in most situations, I'd like to present this as an alternative.

    The PHP exec function probably uses ShellExecute rather than just running it. This looks in the registry to find the associated action for .au3 files.

    A similar thing can be done in java using Desktop.getDesktop().open(SomeFile).

    However, AutoIt allows the user to check at install time whether they want the default action to be edit the script file or run it. Dependant on the setting there, this could mean you end up opening the script in a text editor, rather than running it as intended. ShellExecute (the windows API function) provides an option of what verb to use (default being open). This would allow you to always use the run verb, but means using the windows function from java, which means a wrapper like this. Although it would mean a lot more effort, you'd be guaranteed it would work on any valid AutoIt installation.

    点赞 评论 复制链接分享