Java chama o comando cmd e produz as informações de exibição:
pacote com.anxin.cmd.test; importar java.io.bufferedReader; importar java.io.inputStreamReader; public class Comando {public static void main (string [] args) {try {runtime rt = runtime.getRuntime (); Processo pr = rt.exec ("cmd /c dir"); // cmd/c calc // Process pr = rt.exec ("d: //xunlei//project.aspx"); BufferredReader input = new BufferredReader (new InputStreamReader (PR.GetInputStream (), "GBK")); Linha de string = null; while ((line = input.readline ())! = null) {System.out.println (line); } int exitVal = pr.waitfor (); System.out.println ("saiu do código de erro" + exitVal); } catch (Exceção e) {System.out.println (e.toString ()); E.PrintStackTrace (); }}}Existem três maneiras pelas quais Java inicia o aplicativo nativo exe:
O primeiro método: use o método CMD
/ ** * Execute o comando cmd * * @param comando * @throws ioexception */ public static string executecmd (comando string) lança ioexception {log.info ("comando execute:" + comando); Runtime Runtime = Runtime.getRuntime (); Process de processo = RUNTime.exec ("cmd /c" + comando); BufferErader BR = new BufferredReader (new InputStreamReader (process.getInputStream (), "UTF-8")); Linha de string = null; StringBuilder Build = new StringBuilder (); while ((line = Br.readline ())! = null) {log.info (linha); build.append (linha); } return Build.toString (); } executecmd (Iniciar "Axadwebbrowser" "d: /xadsbrowser/axadwebbrowser.exe"); O segundo método: Use ProcessBuilder para chamar o método CMD
/ ** * Inicie o aplicativo * * @param ProgramName * @return * @throws ioexception */ public static void startProgram (String Programpath) lança ioexception {log.info ("inicie o aplicativo:" + programath); if (stringUtils.isnotblank (programathath)) {try {string programname = ProgramAmath.substring (Programpath.LastIndexof ("/") + 1, ProgramApthath.LastIndexOf ("."); List <String> list = new ArrayList <String> (); list.add ("cmd.exe"); list.add ("/c"); list.add ("start"); list.add ("/" " + programaName +"/""); list.add ("/" " + programath +"/""); Processbuilder pbuilder = new ProcessBuilder (List); pbuilder.start (); } catch (Exceção e) {e.printStackTrace (); Log.error ("Application:" + ProgramAmath + "não existe!"); }}} A terceira maneira: use o desktop para iniciar o aplicativo
/ ** * Inicie o aplicativo * * @param ProgramName * @return * @throws ioexception */ public static void startProgram (String Programpath) lança ioexception {log.info ("inicie o aplicativo:" + programath); if (stringutils.isnotblank (programathath)) {try {desktop.getdesktop (). Open (novo arquivo (programathath)); } catch (Exceção e) {e.printStackTrace (); Log.error ("Application:" + ProgramAmath + "não existe!"); }}}