Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

178
Visualizações
cómo ejecutar el archivo ejecutable c desde java desde linux-bash por "jna" (no desde windows-cmd)

para linux-text, solía hacer tal esfuerzo:

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

pero tuve problemas de entrada y salida.

Esta fue otra sugerencia :

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

Eso fue demasiado largo para probar lo mismo que la solución jni , ¿hay alguna instrucción más simple ?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

No necesita JNA o JNI (excepto según lo implemente el JDK) para ejecutar un comando. Es tan simple como usar Runtime.exec() y capturar la salida.

He implementado esto en una clase aquí . Los bits que necesita se simplifican a continuación.

 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; }

Entonces solo llama

 String[] envp = new String[] { "LC_ALL=C" }; String[] cmd = new String[] { "/path/to/cmd", "arg1", "arg2" }; List<String> output = runNative(cmd, envp);
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda