简体   繁体   English

如何通过“jna”从linux-bash运行java中的c可执行文件(不是来自windows-cmd)

[英]how to run c executable file from java from linux-bash by " jna" (not from windows-cmd)

for linux-text, I used to make such an effort:对于 linux-text,我曾经做过这样的努力:

   ProcessBuilder p = new ProcessBuilder("/path/to/file/c/executable");
   p.start();

but I had trouble input and output.但我在输入和 output 时遇到问题。

This was another suggestion :这是另一个建议

Kernel32.java:内核32.java:

    public interface Kernel32 extends StdCallLibrary {
    
       ----
        };
    
        public Kernel32 INSTANCE = (Kernel32) Native.loadLibrary("Kernel32", Kernel32.class, WIN32API_OPTIONS);
    
    ----
    }

RunTest.java运行测试.java

    public class RunTest {
    
    ---
    
        static HANDLE inputFile = null;
    
        static void createChildProcess(String cmd){
            String szCmdline = cmd;
    
            PROCESS_INFORMATION processInformation = new PROCESS_INFORMATION();
            STARTUPINFO startupInfo = new STARTUPINFO();
  --
            // Create the child process. 
            if (!Kernel32.INSTANCE.CreateProcess(
                --
                System.err.println(Kernel32.INSTANCE.GetLastError());
            }
            else {
                com.sun.jna.platform.win32.Kernel32.INSTANCE.WaitForSingleObject(processInformation.hProcess, 0xFFFFFFFF);
    
                ---
            }
        }
    
        static void WriteToPipe() 
    
        // Stop when there is no more data. 
        { 
            IntByReference dwRead = new IntByReference();
     --
    
            for (;;) 
            { 
    
               --
          
                 }

That was too long to test as same as jni solution , is there a simpler instruction?那太长了,无法像jni 解决方案一样进行测试,是否有更简单的说明?

You don't need JNA or JNI (except as implemented by the JVM) to execute a command.您不需要 JNA 或 JNI(由 JVM 实现的除外)来执行命令。 It's a simple as using Runtime.exec() and capturing the output.使用Runtime.exec()并捕获 output 很简单。

I've implemented this in a class here .我在这里的 class 中实现了这个。 Bits you need are below.您需要的位如下。

public static List<String> runNative(String[] cmdToRunWithArgs, String[] envp) {
    Process p = null;
    try {
        p = Runtime.getRuntime().exec(cmdToRunWithArgs, envp);
        return getProcessOutput(p);
    } catch (SecurityException | IOException e) {
        // handle exception
    } finally {
        // Ensure all resources are released
        // Note: on Solaris must also close streams
        if (p != null) {
            p.destroy();
        }
    }
    return Collections.emptyList(); // or throw exception
}

private static List<String> getProcessOutput(Process p) {
    ArrayList<String> sa = new ArrayList<>();
    try (BufferedReader reader = new BufferedReader(
            new InputStreamReader(p.getInputStream(), Charset.defaultCharset()))) {
        String line;
        while ((line = reader.readLine()) != null) {
            sa.add(line);
        }
        p.waitFor();
    } catch (IOException e) {
        // handle exception
    } catch (InterruptedException ie) {
        // handle exception
        Thread.currentThread().interrupt();
    }
    return sa;
}

Then just call然后打电话

String[] envp = new String[] { "LC_ALL=C" };
String[] cmd = new String[] { "/path/to/cmd", "arg1", "arg2" };
List<String> output = runNative(cmd, envp);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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