here is my program to create wifi...i havent finished it but look at the start method...and start button actionlistener...i am having problem in running "netsh wlan start hostednetwork" command through elevated command prompt...please help as i am having very less experience in java.lang.process and i need to finish the code for my project...
It is giving me output - "exit value is 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
;
}
}