进程在waitFor()中挂起

时间:2011-09-12 12:50:15

标签: java mysql process

我正在尝试运行mysql来从java执行一些文件。 输入是从一个文件中读取的,应该通过管道进入mysql进程,everthing似乎没问题,但行

int exitCode = proc.waitFor(); 

永远不会回来。

private boolean runScript(String path, String cmd, File file) throws IOException, InterruptedException {
        Runtime rt = Runtime.getRuntime();
        Process proc = rt.exec( path + File.separatorChar + cmd );
        OutputStream procOS = proc.getOutputStream();
        InputStream procES = proc.getErrorStream();
        InputStream procIS = proc.getInputStream();
        OutputStreamWriter procStdIn = new OutputStreamWriter( procOS );

        FileInputStream fis = new FileInputStream( file );
        BufferedReader reader = new BufferedReader( new InputStreamReader( fis ) );
        String send;

        while ( ( send = reader.readLine() ) != null ) {
            procStdIn.write( send );
            System.out.println( send );
            copyStream( procES, System.err );
            copyStream( procIS, System.out );
        }
        procStdIn.write( "\r\nexit\r\n" );
        int exitCode = proc.waitFor();
        return exitCode == 0;
    }

    private void copyStream(InputStream is, PrintStream err) throws IOException {
        byte b[] = new byte[ 1024 ];
        int length;
        while ( is.available() > 0 && ( length = is.read( b ) ) > 0 ) {
            err.write( b, 0, length );
        }

}

2 个答案:

答案 0 :(得分:3)

我怀疑你正在阻止阅读stdout和stderr。有关详细信息,请参阅this answer

答案 1 :(得分:0)

添加:

procStdIn.write( "\r\nexit\r\n" );

后:

<button class="btn default-btn advancedbtn" ng-click="vm.openModal()">Advanced</button>

 angular.module('crm.ma').controller('ModalDemoCtrl', function ($scope, $modal, $log) {

    $scope.items = ['item1', 'item2', 'item3'];

    $scope.animationsEnabled = true;

    $scope.openModal = function (size) {

        var modalInstance = $modal.open({
            animation: $scope.animationsEnabled,
            templateUrl: 'topnav_advancedmodal.html',
            controller: 'ModalInstanceCtrl',
            size: size,
            resolve: {
                items: function () {
                    return $scope.items;
                }
            }
        });

        modalInstance.result.then(function (selectedItem) {
            $scope.selected = selectedItem;
        }, function () {
            $log.info('Modal dismissed at: ' + new Date());
        });
    };



});