【问题标题】:Execute Shell Script from Flex for mac os从 Flex for mac os 执行 Shell 脚本
【发布时间】:2014-02-23 23:19:42
【问题描述】:

我在这里处理的是 NativeProcess,特别是 mac os 中的命令。所以,我在与我的应用程序相同的目录中有一个 .sh 文件。通过这个 shell 脚本,我正在尝试执行一个 .jarfile(同样的目录) .不知何故,我犯了一些错误,这就是它不起作用的原因。

我的代码是下一个

    <?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 时没有什么可做的。如果您发现任何奇怪或错误的地方,请告诉我。

谢谢

【问题讨论】:

    标签: java macos actionscript-3 shell apache-flex


    【解决方案1】:

    这是可以正常工作的代码

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

    【讨论】:

      【解决方案2】:

      您是否在这里尝试过,是否遇到任何错误? http://forums.adobe.com/thread/721724

      【讨论】:

        猜你喜欢
        • 2010-09-21
        • 2011-03-02
        • 1970-01-01
        • 2011-06-03
        • 1970-01-01
        • 2019-02-04
        • 2014-09-29
        • 2013-06-13
        • 1970-01-01
        相关资源
        最近更新 更多