这是我的程序来创建wifi ...我还没有完成它,但看看启动方法...并启动按钮actionlistener ...我在运行中遇到问题" netsh wlan start hostednetwork"命令通过提升的命令提示...请帮助,因为我在java.lang.process中的经验非常少,我需要完成我的项目的代码......
它给了我输出 - "退出值是1"
import java.awt.*;
import java.lang.ProcessBuilder;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.Arrays;
public class create implements ActionListener{
JFrame frame;
JFrame frame2;
//for frame
JButton button1;
JButton button2;
JButton button3;
JPanel panel1;
JPanel panel2;
JLabel label1;
JLabel label2;
JLabel label3;
//for frame2
JPanel panel3;
JTextField name;
JPasswordField pass;
JButton okbutton;
JLabel namelabel;
JLabel passlabel;
cmdstuff cmdobj;
public static void main(String args[]){
create mainObj=new create();
mainObj.go();
}
public void go(){
frame=new JFrame("WiFi");
frame2=new JFrame("Customize");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,175);
button1=new JButton("Customize");
button2=new JButton("Start");
button3=new JButton("Stop");
button2.setAlignmentX(Component.CENTER_ALIGNMENT);
button3.setAlignmentX(Component.CENTER_ALIGNMENT);
button1.setAlignmentX(Component.CENTER_ALIGNMENT);
panel1=new JPanel();
panel2=new JPanel();
label1=new JLabel("Status:");
label2=new JLabel("Click from above button to change status");
label3=new JLabel("Just click on the button and see it done:");
frame.getContentPane().add(BorderLayout.NORTH,label3);
panel1.add(button1);
panel1.add(button2);
panel1.add(button3);
panel1.setLayout(new BoxLayout(panel1,BoxLayout.Y_AXIS));
panel2.add(label1);
panel2.add(label2);
panel2.setLayout(new BoxLayout(panel2,BoxLayout.Y_AXIS));
frame.getContentPane().add(BorderLayout.CENTER,panel1);
frame.getContentPane().add(BorderLayout.SOUTH,panel2);
frame.setVisible(true);
//frame2
panel3 =new JPanel();
name=new JTextField(10);
pass=new JPasswordField(10);
okbutton=new JButton("OK");
namelabel=new JLabel("Your Wifi Hostspot's Name: ");
passlabel=new JLabel("Your Wifi Hostspot's Password:");
//namelabel.setLabelFor(name);
//passlabel.setLabelFor(pass);
FlowLayout flow=new FlowLayout();
panel3.setLayout(flow);
panel3.add(namelabel);
panel3.add(name);
panel3.add(passlabel);
panel3.add(pass);
frame2.getContentPane().add(BorderLayout.NORTH,new JLabel("Enter the details and click ok:"));
frame2.getContentPane().add(BorderLayout.CENTER,panel3);
frame2.getContentPane().add(BorderLayout.SOUTH,okbutton);
frame2.setSize(400,150);
//actionlisener setting
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
okbutton.addActionListener(this);
//button1.addActionListener(this);
}
//start wifi method
public void start() throws IOException{
String[] command = {"CMD","runas /user:Akshay\\administrator cmd", "netsh wlan start hostednetwork"};
ProcessBuilder probuilder = new ProcessBuilder( command );
Process process = probuilder.start();
//Read out dir output
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
System.out.printf("Output of running %s is:\n",
Arrays.toString(command));
while ((line = br.readLine()) != null) {
System.out.println(line);
}
//Wait to get exit value
try {
int exitValue = process.waitFor();
System.out.println("\n\nExit Value is " + exitValue);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//action performed
public void actionPerformed(ActionEvent e){
if(e.getActionCommand()=="Customize")
{frame2.setVisible(true);
}
else if(e.getActionCommand()=="OK")
{frame2.setVisible(false);
String wifiname=name.getText();
String wifipass=pass.getText();
System.out.println(wifiname);
System.out.println(wifipass);
}
else if(e.getActionCommand()=="Start")
{
try{
create secObj=new create();
secObj.start();
}catch(Exception i){
};
}
else if(e.getActionCommand()=="Stop")
{System.out.println("stop pressed");}
else
;
}
}
答案 0 :(得分:0)
虽然我现在不在Windows上并且无法对此进行测试,但我认为只有一个问题就是在cmd
中运行命令,您需要说
cmd /C my-command
你还没有得到/ C.但是,我不知道你在该命令中使用cmd
的原因; How to run exec from Java for netsh?没有,但它没有解释它如何获得所需的权限。 Elevating a ProcessBuilder process via UAC?确实描述了如何获得所需的权限。