Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
实施",;系统“;Java中的命令_Java_Javascript_Process_System_Subprocess - Fatal编程技术网

实施",;系统“;Java中的命令

实施",;系统“;Java中的命令,java,javascript,process,system,subprocess,Java,Javascript,Process,System,Subprocess,我需要一个“系统”函数调用,与Python、Perl、PHP、Ruby和c中的函数调用相同。当它在Rhino JavaScript引擎上运行时,它将成为名为Narhall的JavaScript标准库的一个组件,Rhino JavaScript引擎反过来在Java上运行 问题是Java的标准库似乎已经抽象掉了生成共享父进程stdio的子进程的能力。这意味着您不能将交互推迟到子流程 我在这方面的第一次尝试是实现Python的subprocess.popen。这使用三个“pumper”线程独立地主动复

我需要一个“系统”函数调用,与Python、Perl、PHP、Ruby和c中的函数调用相同。当它在Rhino JavaScript引擎上运行时,它将成为名为Narhall的JavaScript标准库的一个组件,Rhino JavaScript引擎反过来在Java上运行

问题是Java的标准库似乎已经抽象掉了生成共享父进程stdio的子进程的能力。这意味着您不能将交互推迟到子流程

我在这方面的第一次尝试是实现Python的subprocess.popen。这使用三个“pumper”线程独立地主动复制父进程的stdio(以防止死锁)。不幸的是,这给了我们两个问题。首先,当子进程自动退出时,输入不会自动关闭。其次,到子进程的流没有正确地缓冲和刷新

我正在寻找能够使require(“os”).system()命令按预期工作的解决方案

该项目正在进行中

相关代码:


您需要通过轮询退出代码或让一个单独的线程在方法中等待来分别监视进程退出状态


关于缓冲和刷新流的问题,我认为没有一个简单的解决方案。有几个Java类以不同的形式进行缓冲(,等等)。也许其中一个可以提供帮助?

不确定这是否是您正在寻找的,但您可以通过以下命令调用C
系统
函数:


总之,粗略的测试在Windows上运行。

如果我正确理解您的意思,您希望这样:

import java.util.*;
import java.io.*;
class StreamGobbler extends Thread
{
    InputStream is;
    String type;
    OutputStream os;

    StreamGobbler(InputStream is, String type)
    {
        this(is, type, null);
    }
    StreamGobbler(InputStream is, String type, OutputStream redirect)
    {
        this.is = is;
        this.type = type;
        this.os = redirect;
    }

    public void run()
    {
        try
        {
            PrintWriter pw = null;
            if (os != null)
                pw = new PrintWriter(os);

            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);
            String line=null;
            while ( (line = br.readLine()) != null)
            {
                if (pw != null)
                    pw.println(line);
                System.out.println(type + ">" + line);    
            }
            if (pw != null)
                pw.flush();
        } catch (IOException ioe)
            {
            ioe.printStackTrace();  
            }
    }
}
public class GoodWinRedirect
{
    public static void main(String args[])
    {
        if (args.length < 1)
        {
            System.out.println("USAGE java GoodWinRedirect <outputfile>");
            System.exit(1);
        }

        try
        {            
            FileOutputStream fos = new FileOutputStream(args[0]);
            Runtime rt = Runtime.getRuntime();
            Process proc = rt.exec("java jecho 'Hello World'");
            // any error message?
            StreamGobbler errorGobbler = new 
                StreamGobbler(proc.getErrorStream(), "ERROR");            

            // any output?
            StreamGobbler outputGobbler = new 
                StreamGobbler(proc.getInputStream(), "OUTPUT", fos);

            // kick them off
            errorGobbler.start();
            outputGobbler.start();

            // any error???
            int exitVal = proc.waitFor();
            System.out.println("ExitValue: " + exitVal);
            fos.flush();
            fos.close();        
        } catch (Throwable t)
          {
            t.printStackTrace();
          }
    }
}
import java.util.*;
导入java.io.*;
类StreamGobbler扩展线程
{
输入流为;
字符串类型;
输出流;
StreamGobbler(InputStream是,字符串类型)
{
这(is,type,null);
}
StreamGobbler(InputStream是,字符串类型,OutputStream重定向)
{
this.is=is;
this.type=type;
this.os=重定向;
}
公开募捐
{
尝试
{
PrintWriter pw=null;
如果(os!=null)
pw=新的打印机(os);
InputStreamReader isr=新的InputStreamReader(is);
BufferedReader br=新的BufferedReader(isr);
字符串行=null;
而((line=br.readLine())!=null)
{
如果(pw!=null)
pw.println(行);
系统输出打印项次(类型+“>”+行);
}
如果(pw!=null)
pw.flush();
}捕获(ioe异常ioe)
{
ioe.printStackTrace();
}
}
}
公共类GoodWinRedirect
{
公共静态void main(字符串参数[])
{
如果(参数长度<1)
{
System.out.println(“用法java GoodWinRedirect”);
系统出口(1);
}
尝试
{            
FileOutputStream fos=新的FileOutputStream(args[0]);
Runtime rt=Runtime.getRuntime();
processproc=rt.exec(“javajecho'helloworld'”);
//有错误信息吗?
StreamGobbler errorGobbler=新建
StreamGoBler(proc.getErrorStream(),“ERROR”);
//有输出吗?
StreamGobbler outputGobbler=新建
StreamGobbler(proc.getInputStream(),“OUTPUT”,fos);
//把他们踢出去
errorGobbler.start();
outputGobbler.start();
//有错误吗???
int exitVal=proc.waitFor();
System.out.println(“ExitValue:+exitVal”);
fos.flush();
fos.close();
}捕获(可丢弃的t)
{
t、 printStackTrace();
}
}
}
我在中发现了这段代码:不久前,我正在寻找一个类似的解决方案来包装对一些exe文件的系统调用


从那以后,我的代码有了一些改进,但我认为这是一个很好的例子。

同时使用流程标准输出和错误是非常重要的。请参见此处其他地方的Carlos Tasada示例代码


如果不这样做,代码可能会(也可能不会)工作,这取决于派生进程的输出。当输出发生变化时(比如说,如果生成的进程遇到错误),如果没有并发消耗,进程将死锁。我在上看到的与
Process.exec()
相关的大多数问题都与阻塞相关。

添加jni.jar,这一点JavaScript就起到了作用。非常感谢。var jna=Packages.com.sun.jna;var clib=jna.nativellibrary.getInstance(jna.Platform.isWindows()?“msvcrt”:“c”);var csystem=clib.getFunction(“系统”);调用([“echo Hello,World!”);
import java.util.*;
import java.io.*;
class StreamGobbler extends Thread
{
    InputStream is;
    String type;
    OutputStream os;

    StreamGobbler(InputStream is, String type)
    {
        this(is, type, null);
    }
    StreamGobbler(InputStream is, String type, OutputStream redirect)
    {
        this.is = is;
        this.type = type;
        this.os = redirect;
    }

    public void run()
    {
        try
        {
            PrintWriter pw = null;
            if (os != null)
                pw = new PrintWriter(os);

            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);
            String line=null;
            while ( (line = br.readLine()) != null)
            {
                if (pw != null)
                    pw.println(line);
                System.out.println(type + ">" + line);    
            }
            if (pw != null)
                pw.flush();
        } catch (IOException ioe)
            {
            ioe.printStackTrace();  
            }
    }
}
public class GoodWinRedirect
{
    public static void main(String args[])
    {
        if (args.length < 1)
        {
            System.out.println("USAGE java GoodWinRedirect <outputfile>");
            System.exit(1);
        }

        try
        {            
            FileOutputStream fos = new FileOutputStream(args[0]);
            Runtime rt = Runtime.getRuntime();
            Process proc = rt.exec("java jecho 'Hello World'");
            // any error message?
            StreamGobbler errorGobbler = new 
                StreamGobbler(proc.getErrorStream(), "ERROR");            

            // any output?
            StreamGobbler outputGobbler = new 
                StreamGobbler(proc.getInputStream(), "OUTPUT", fos);

            // kick them off
            errorGobbler.start();
            outputGobbler.start();

            // any error???
            int exitVal = proc.waitFor();
            System.out.println("ExitValue: " + exitVal);
            fos.flush();
            fos.close();        
        } catch (Throwable t)
          {
            t.printStackTrace();
          }
    }
}