Boa tarde, gostaria de saber como posso estar utilizando comandos do prompt dentro de meus programas java… como por exemplo um net send?
Se tiverem algum exemplo…
Obrigado!!
Boa tarde, gostaria de saber como posso estar utilizando comandos do prompt dentro de meus programas java… como por exemplo um net send?
Se tiverem algum exemplo…
Obrigado!!
Runtime.getRuntime().exec().
Pesquisa por “Runtime” no forum que ja rolaram varios topicos sobre isso - inclusive eu, que nunca precisei disso, ja aprendi por osmose como fazer, apenas lendo os topicos do forum.
Marcio Kuchma
Eu fiz isso pra uns comandos em linux…
Process process = Runtime.getRuntime().exec("sudo fdisk -l");
BufferedReader ler = new BufferedReader(new InputStreamReader(process.getInputStream()));
String texto = "";
while((texto = ler.readLine()) != null) {
sysout(texto);
}
Esse eh um caminho daniel Sam.
Como dizia o meu professor de Java: “Java é que nem neston, existem mil de se preparar maneiras, invente a sua.”
VELO
import javax.swing.;
import java.io.;
import java.awt.;
import java.awt.event.;
import java.util.;
import java.text.;
import java.util.regex.*;
//
public class NetSendGUI extends JFrame implements ActionListener{
//
private JComboBox to = new JComboBox();
private JTextArea message = new JTextArea(10,30);
private JButton btnRefresh = new JButton(“Refresh”);
private JButton btnSend = new JButton(“Send”);
private JLabel lblStatus = new JLabel();
/* remove trailing whitespace */
public static String rtrim(String source) {
return source.replaceAll("\\s+$", "");
}
public void showException(Exception e){
JOptionPane.showMessageDialog(null,e.toString());
}
/****/
public void loadTo(){
//
setTitle("NetSendGUI by HLINO");
//clean
to.removeAll();
try {
// Execute command
String command = "cmd /c net view";
Process child = Runtime.getRuntime().exec(command);
// Get the input stream and read from it
InputStream in = child.getInputStream();
int c;
String line = new String();
//
while ((c = in.read()) != -1) {
//enter
if((char)c == '\n'){
//startsWith
if(line.indexOf("\\") != -1){
rtrim(line);
line = line.replaceAll("\n","");
line = line.replace("\\","");
to.addItem(line);
}
//
line = "";
}
//
line = line + (char)c;
}
in.close();
} catch (IOException e) {
showException(e);
}
}
/****/
public String netSend(){
String line = new String();
try{
String ip = to.getSelectedItem().toString();
String command = "cmd /c net send " + ip + " \"" + message.getText() +"\"";
System.out.println(command);
Process child = Runtime.getRuntime().exec(command);
// Get the input stream and read from it
InputStream in = child.getInputStream();
int c;
//
while ((c = in.read()) != -1) {
//
line = line + (char)c;
}
in.close();
}catch(IOException e){
showException(e);
}
return line;
}
/****/
public NetSendGUI(){
//
setLayout( new BorderLayout() );
//
add(to, BorderLayout.NORTH);
add(new JScrollPane(message), BorderLayout.CENTER);
JPanel pSouth = new JPanel();
pSouth.setLayout( new BorderLayout() );
JPanel pButton = new JPanel();
//
pButton.add(btnRefresh);
pButton.add(btnSend);
//
btnSend.setMnemonic('s');
btnRefresh.setMnemonic('r');
pSouth.add(lblStatus, BorderLayout.NORTH);
pSouth.add(pButton, BorderLayout.CENTER);
//
add(pSouth, BorderLayout.SOUTH);
//
loadTo();
//
to.addActionListener(this);
btnRefresh.addActionListener(this);
btnSend.addActionListener(this);
//
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
//pack
pack();
//center
setLocationRelativeTo(null);
//visible
setVisible(true);
}
/****/
public void refresh(){
loadTo();
this.lblStatus.setText("");
this.to.requestFocus(true);
}
/**
* @param message
**/
public void setStatus(String message){
Date d = new Date();
SimpleDateFormat sdf = new SimpleDateFormat ("HH:mm:ss");
this.lblStatus.setText( "[" + sdf.format( d ) + "] : " + message);
}
/**
* @param e
**/
public void actionPerformed(ActionEvent e){
if(e.getSource() == to){
message.requestFocus(true);
}
else if(e.getSource() == btnSend){
String sReturn = netSend();
setStatus(sReturn);
message.requestFocus(true);
}
else if(e.getSource() == btnRefresh){
refresh();
}
}
/**
* @param args
**/
public static void main(String args[]){
//
new NetSendGUI();
}
}