How do I keep CFEXECUTE from hanging after a PrintStackTrace

前端 未结 1 465
野趣味
野趣味 2020-12-02 02:34

I\'m using ColdFusion MX7 to perform a CFEXECUTE on some Java 6 code.

Unfortunately, since CF7 does not work under JDK 6 I must do it this way.

My problem

相关标签:
1条回答
  • 2020-12-02 03:27

    Yes, e.printStackTrace(); writes to stderr (standard error stream). Since cfexecute does not capture stderr, that is probably what is causing cfexecute to hang. There was a patch to fix this behavior in CF8.

    Since you are using 7, try Ben Forta's tips about:

    • Redirecting stderr to stdout: A Couple Of Gotchas
    • Flags to run commands and terminate upon completion: Using CFEXECUTE To Execute Command Line Utilities

    Using both /c and 2>&1 should get rid of the hanging problem.

    Update: Added Example

    ColdFusion Code:

    <cftry>  
        <cfset argString = '/c "C:\Program Files\Java\jdk1.6.0_13\bin\java.exe" -cp c:\myJar.jar TestStdErr 2>&1'  >  
        <cfexecute name="c:\windows\system32\cmd.exe" 
            arguments="#argString#"    
            outputFile="c:\cfexcuteResults.log" 
            timeout="5" />  
        <cfcatch>  
           <cfdump var="#cfcatch#">  
        </cfcatch>  
    </cftry>  
    

    Java Class:

    public class TestStdErr {
        public static void main(String[] args) {
            try {
                // cause a divide by zero exception 
                int a = 0;
                int b = 2 /a;
             }
             catch(Exception e){
                e.printStackTrace();
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题