简体   繁体   中英

Execute Shell Script from Flex for mac os

I am dealing here with NativeProcess and specificly with commands in mac os.So, i have a .sh file in same directory as my application.By this shell script i am trying to execute a .jarfile (same directory as well). Somehow i am doing some mistake thats why it didn't work.

my code is next

    <?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>

next is shell scripting part

java -jar Test.jar

when i am testing this app on a mac system, app display alert box (regards .sh File Path) but nothing else happening next.

There is not much to walk on when working with NativeProcesses.if you see anything wierd or wrong,please let me know.

Thank you

Did you try this here, were you getting any error? http://forums.adobe.com/thread/721724

Here is code that works fine

    <?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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM