Bom dia amigos, eu estou presisando de ajuda para executar um commando do MsDos e Shell do linux.
como eu executudo alguma linha de comando do MsDos no java?
como eu executudo alguma linha de comando do ShellLinux no java?
Bom dia amigos, eu estou presisando de ajuda para executar um commando do MsDos e Shell do linux.
como eu executudo alguma linha de comando do MsDos no java?
como eu executudo alguma linha de comando do ShellLinux no java?
Runtime rt = Runtime.getRuntime();
Process pros = rt.exec("comando msdos ou linux");
No código abaixo, quando você chama o método getProcessoWindows() você obtém um processo que representa o prompt do DOS...
Dá uma olhada na classe e veja, no fim, exemplos de utilização da mesma...
import java.io.*;
public class ExecutaComandoNativo
{
private final String COMANDO_WINDOWS = "cmd";
private Process processoWindows;
private BufferedReader saidaPadraoWindows;
private BufferedOutputStream entradaPadraoWindows;
private BufferedReader saidaErroWindows;
public ExecutaComandoNativo()
{
}
public Process getProcessoWindows() throws IOException
{
if(processoWindows == null)
{
processoWindows = Runtime.getRuntime().exec(COMANDO_WINDOWS);
}
return processoWindows;
}
public BufferedOutputStream getEntradaPadraoWindows() throws IOException
{
if(entradaPadraoWindows == null)
{
entradaPadraoWindows = new BufferedOutputStream(getProcessoWindows().getOutputStream());
}
return entradaPadraoWindows;
}
public BufferedReader getSaidaErroWindows() throws IOException
{
if(saidaErroWindows == null)
{
saidaErroWindows = new BufferedReader(new InputStreamReader(getProcessoWindows().getErrorStream()));
}
return saidaErroWindows;
}
public BufferedReader getSaidaPadraoWindows() throws IOException
{
if(saidaPadraoWindows == null)
{
saidaPadraoWindows = new BufferedReader(new InputStreamReader(getProcessoWindows().getInputStream()));
}
return saidaPadraoWindows;
}
public void executarComando(String comando)throws IOException
{
getEntradaPadraoWindows().write(comando.getBytes());
getEntradaPadraoWindows().flush();
getEntradaPadraoWindows().close();
}
public void finalizarProcessoWindows()throws Exception
{
entradaPadraoWindows = null;
saidaPadraoWindows = null;
saidaErroWindows = null;
processoWindows.destroy();
processoWindows = null;
}
public static void main(String[] s)
{
ExecutaComandoNativo executaComandoNativo = new ExecutaComandoNativo();
String FFMPEG = "C:\\ffmpeg\\ffmpeg.exe";
try
{
//String comando = "copy \""+documento.getCaminho()+"\" \""+documento.getCaminhoClearcase()+"\"\n";
//String comando = "dir c: \n";
//String comando = "ffmpeg -formats\n";
//String comando = "ffmpeg \n";
//String comando = "ffmpeg -i C:\\Joel Luis Carbonera\\Programação - Projetos\\Projetos\\Projetos em Java\\Projeto - Indexador de informações em arquivos multimídia\\Arquivos de teste\\Arquivos de teste 1\\videos\\teste.mpg convertido.mpeg \n";
String arquivoASerVonvertido = "C:\\Arquivos de teste\\videos\\Pixar - Ice Age Extra.mpg";
String comando = FFMPEG+" -i "+arquivoASerVonvertido+" -f mpeg c:\\conv.mpg\n";
executaComandoNativo.executarComando(comando);
(executaComandoNativo.getProcessoWindows()).waitFor();
String saida;
while((saida = executaComandoNativo.getSaidaPadraoWindows().readLine()) != null)
{
System.out.println(saida);
}
while( (saida = executaComandoNativo.getSaidaErroWindows().readLine()) != null )
{
System.err.println(saida);
}
/*
if((executaComandoNativo.getProcessoWindows()).exitValue()!=null)
{
executaComandoNativo.finalizarProcessoWindows();
}
*/
}
catch(Exception ioe)
{
System.out.println("Problema 2");
ioe.printStackTrace();
}
}
}