2014-01-31 303 views
0

我在這裏處理NativeProcess,特別是使用mac os中的命令。所以,我在與我的應用程序相同的目錄中有一個.sh文件。通過這個shell腳本,我試圖執行.jarfile(同樣的目錄)。不知何故,我做了一些錯誤,那爲什麼它不起作用。從mac執行Shell腳本

我的代碼是未來

<?xml version="1.0" encoding="utf-8"?> 
    <s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
          xmlns:s="library://ns.adobe.com/flex/spark" 
          xmlns:mx="library://ns.adobe.com/flex/mx" 
          creationComplete="init()"> 
     <fx:Declarations> 
      <!-- Place non-visual elements (e.g., services, value objects) here --> 
     </fx:Declarations> 
     <fx:Script> 
      <![CDATA[ 
       import mx.controls.Alert; 
       var process:NativeProcess; 
       private function init():void 
       { 
        if (NativeProcess.isSupported) 
        { 
         Alert.show("suport native process."); 
          setupAndLaunch(); 
        } 
       } 
       private function setupAndLaunch():void 
       { 
       var cmdFile:File = File.applicationDirectory.resolvePath("InvokeJar.sh"); 
       Alert.show(cmdFile.nativePath,".Sh File Path"); 
       var processArgs:Vector.<String> = new Vector.<String>; 
       processArgs.push('chmod -x "'+cmdFile.nativePath+'"'); 

        var nativeProcessStartupInfo:NativeProcessStartupInfo = new NativeProcessStartupInfo(); 
       nativeProcessStartupInfo.arguments = processArgs; 
       nativeProcessStartupInfo.executable = cmdFile; 
       nativeProcessStartupInfo.workingDirectory = File.applicationDirectory; 

        process = new NativeProcess(); 
        process.start(nativeProcessStartupInfo); 
        process.addEventListener(ProgressEvent.STANDARD_OUTPUT_DATA, onOutputData); 
        process.addEventListener(ProgressEvent.STANDARD_ERROR_DATA, onErrorData); 
        process.addEventListener(NativeProcessExitEvent.EXIT, onExit); 
        process.addEventListener(IOErrorEvent.STANDARD_OUTPUT_IO_ERROR, onIOError); 
        process.addEventListener(IOErrorEvent.STANDARD_ERROR_IO_ERROR, onIOError); 
       } 

       public function onOutputData(event:ProgressEvent):void 
       { 
        trace("Got: ", process.standardOutput.readUTFBytes(process.standardOutput.bytesAvailable)); 
       } 

       public function onErrorData(event:ProgressEvent):void 
       { 
        trace("ERROR -", process.standardError.readUTFBytes(process.standardError.bytesAvailable)); 
       } 

       public function onExit(event:NativeProcessExitEvent):void 
       { 
        trace("Process exited with ", event.exitCode); 
       } 

       public function onIOError(event:IOErrorEvent):void 
       { 
        trace(event.toString()); 
       } 
      ]]> 
     </fx:Script> 
    </s:WindowedApplication> 

下一個是shell腳本的一部分

java -jar Test.jar 

,當我在Mac系統上測試該應用程序,應用程序顯示警告對話框(視.SH文件路徑),但沒有其他事情發生。

當使用NativeProcesses時,沒有太多東西需要解決。如果您發現任何更奇怪或錯誤的東西,請告訴我。

謝謝

回答

0

這裏是代碼工作正常

<?xml version="1.0" encoding="utf-8"?> 
     <s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
           xmlns:s="library://ns.adobe.com/flex/spark" 
           xmlns:mx="library://ns.adobe.com/flex/mx" 
           creationComplete="init()"> 
      <fx:Declarations> 
       <!-- Place non-visual elements (e.g., services, value objects) here --> 
      </fx:Declarations> 
      <fx:Script> 
       <![CDATA[ 
        import mx.controls.Alert; 
        var process:NativeProcess; 
        private function init():void 
        { 
         if (NativeProcess.isSupported) 
         { 
          Alert.show("suport native process."); 
           setupAndLaunch(); 
         } 
        } 
        private function setupAndLaunch():void 
        { 
        var shFilePath:String= File.applicationDirectory.resolvePath('invokeJAR.sh').nativePath; 
         Alert.show(shFilePath,".Sh File Path"); 
         var cmdFile:File=new File('/bin/bash'); 
         var processArgs:Vector.<String>= new Vector.<String>; 
         processArgs.push('-c');   
         processArgs.push('bash invokeJAR.sh'); 
         var nativeProcessStartupInfo:NativeProcessStartupInfo = new NativeProcessStartupInfo(); 
        nativeProcessStartupInfo.arguments = processArgs; 
        nativeProcessStartupInfo.executable = cmdFile; 
        nativeProcessStartupInfo.workingDirectory = File.applicationDirectory; 

         process = new NativeProcess(); 
         process.start(nativeProcessStartupInfo); 
         process.addEventListener(ProgressEvent.STANDARD_OUTPUT_DATA, onOutputData); 
         process.addEventListener(ProgressEvent.STANDARD_ERROR_DATA, onErrorData); 
         process.addEventListener(NativeProcessExitEvent.EXIT, onExit); 
         process.addEventListener(IOErrorEvent.STANDARD_OUTPUT_IO_ERROR, onIOError); 
         process.addEventListener(IOErrorEvent.STANDARD_ERROR_IO_ERROR, onIOError); 
       try 
       { 
        process.start(nativeProcessStartupInfo); 
       } 
       catch(e:Error) 
       { 
        Alert.show(e.message,"startup Error"); 
       } 
        } 

        public function onOutputData(event:ProgressEvent):void 
        { 
         trace("Got: ", process.standardOutput.readUTFBytes(process.standardOutput.bytesAvailable)); 
        } 

        public function onErrorData(event:ProgressEvent):void 
        { 
         trace("ERROR -", process.standardError.readUTFBytes(process.standardError.bytesAvailable)); 
        } 

        public function onExit(event:NativeProcessExitEvent):void 
        { 
         trace("Process exited with ", event.exitCode); 
        } 

        public function onIOError(event:IOErrorEvent):void 
        { 
         trace(event.toString()); 
        } 
       ]]> 
      </fx:Script> 
     </s:WindowedApplication>