简体   繁体   中英

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:

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

but I had trouble input and output.

This was another 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 (;;) 
            { 
    
               --
          
                 }

That was too long to test as same as jni solution , is there a simpler instruction?

You don't need JNA or JNI (except as implemented by the JVM) to execute a command. It's a simple as using Runtime.exec() and capturing the output.

I've implemented this in a class here . 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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