【问题标题】:how to run c executable file from java from linux-bash by " jna" (not from windows-cmd)如何通过“jna”(不是从 windows-cmd)从 linux-bash 运行 java 的 c 可执行文件
【发布时间】:2022-01-21 22:56:15
【问题描述】:

对于linux-text,我曾经做过这样的努力:

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

但我在输入和输出方面遇到了麻烦。

这是另一个suggestion

Kernel32.java:

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

RunTest.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 (;;) 
            { 
    
               --
          
                 }

太长了,无法和jni解决方案一样测试,有没有更简单的说明?

【问题讨论】:

    标签: java linux executable jna processbuilder


    【解决方案1】:

    您不需要 JNA 或 JNI(由 JDK 实现的除外)来执行命令。使用Runtime.exec() 并捕获输出很简单。

    我已经在 here 类中实现了这一点。您需要的位简化如下。

    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
            if (p != null) {
                p.destroy(); // also closes streams except...
                // on Windows and Solaris must close streams separately
            }
        }
        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;
    }
    

    然后打电话

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

    【讨论】:

      猜你喜欢
      • 2016-12-19
      • 1970-01-01
      • 2015-10-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多