0

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
            ;
    }
}
AkshayM
  • 1,421
  • 10
  • 21
  • You need to say what the problem is that you're having with "netsh wlan start hostednetwork". If it's a syntax error, did you notice the two quotation marks before `runas` on that line? – GKFX Feb 07 '16 at 18:12
  • @GKFX Oops sorry I will check gain removing that quotation and let u know the result...as I am currently not on my dev station... – AkshayM Feb 09 '16 at 04:43
  • @GKFX I have edited the question please have a look...it is not giving a desired output... – AkshayM Feb 10 '16 at 14:46

1 Answers1

0

Although I'm not on Windows now and can't test this, I think that one problem is simply that to run a command in cmd, you need to say

cmd /C my-command

and you haven't got a /C. However, I don't know why you're using cmd in that command; How to run exec from Java for netsh? goes without, although it doesn't explain how it gets the needed privileges. Elevating a ProcessBuilder process via UAC? does describe how to get the needed privileges.

Community
  • 1
  • 1
GKFX
  • 1,386
  • 1
  • 11
  • 30