本文介绍了Java在Windows 8中创建WiFi热点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我需要在Windows 7-8中使用Java创建一个WiFi热点和一个DHCP服务器。 我正在开发一个软件,可以为用户较少的侵入和简单,我可以。 对于我的工作的第一部分,我想使用netsh创建托管网络并设置静态ip。 我当前的代码是: c> c> $ 而不是运行: try { System.out.println( - Setting up WLAN - ); String netshCommand =netsh wlan set hostednetwork mode = allow ssid = \YourSSID\key = \YourPassword\& exit; String [] elevateCommand = new String [] {./ Release / elevate.exe,-wait,-k,prog,netshCommand}; ProcessBuilder pb1 = new ProcessBuilder(elevateCommand); Process p1 = pb1.start(); p1.waitFor(); System.out.println( - Starting WLAN - ); netshCommand =netsh wlan start hostednetwork& exit; elevateCommand = new String [] {./ Release / elevate.exe,-wait,-k,prog,netshCommand}; ProcessBuilder pb2 = new ProcessBuilder(elevateCommand); Process p2 = pb2.start(); p2.waitFor(); System.out.println( - 设置IPv4接口 - ); netshCommand =netsh interface ipv4 set address \Conexiónde redinalámbrica\static 192.168.0.102 255.255.255.0 192.168.0.254& exit; elevateCommand = new String [] {./ Release / elevate.exe,-wait,-k,prog,netshCommand}; ProcessBuilder pb3 = new ProcessBuilder(elevateCommand); Process p3 = pb3.start(); p3.waitFor(); System.out.println( - 获取IPv4接口转储 - ); netshCommand =netsh interface ipv4 dump; ProcessBuilder pb4 = new ProcessBuilder(cmd.exe,/ c,netshCommand); Process p4 = pb4.start(); System.out.println( - 打印IPv4接口转储 - ); BufferedReader bfr = new BufferedReader(new InputStreamReader(p4.getInputStream(),ISO-8859-1)); 字符串输出; while((output = bfr.readLine())!= null){ System.out.println(output); } } catch(IOException | InterruptedException ex){ ex.printStackTrace(); } } >:请注意 netshCommand 结尾为&退出。这是因为如果不这样,则 elevate.exe 打开的 cmd 控制台将保持打开状态。 输出应该看起来像这样(对于西班牙语单词,但我有这种语言的Windows): - 启动WLAN - - 设置IPv4接口 - - 获取IPv4接口转储 - - 打印IPv4接口转储 - #------------------------ ---------- #Configuraciónde IPv4 #--------------------------- ------- pushd interface ipv4 reset set global icmpredirects = enabled add route prefix = 0.0.0.0 / 0 interface =Conexiónde redinalámbricanexthop = 192.168.0.254 metric = 1 publish =Sí add address name =Conexiónde redinalámbricaaddress = 192.168.0.102 mask = 255.255.255.0 popd #Fin de laconfiguraciónde IPv4 I need to create a wifi hotspot and a DHCP server in Windows 7-8 with Java. I'm developing a software that could be for the user less intrusive and simple that i could.For the first part of my work, I thought to use netsh to create the hosted network and to set static the ip.my current code is this:String[] command = { "cmd", };Process p = Runtime.getRuntime().exec(command);new Thread(new SyncPipe(p.getErrorStream(), System.err)).start();new Thread(new SyncPipe(p.getInputStream(), System.out)).start();PrintWriter stdin = new PrintWriter(p.getOutputStream());//now I use the stdin.println for my shell commandsstdin.println("netsh wlan set hostednetwork mode=allow ssid=mynetwork key=mypassword");stdin.println("netsh wlan start hostednetwork");stdin.println("netsh interface ipv4 set address \"Wi-Fi\" static 192.168.1.2 255.255.255.0 192.168.1.254");Problems:1) I need to elevate the privileges to administrator.I tried to use "elevate.exe" http://jpassing.com/2007/12/08/launch-elevated-processes-from-the-command-line/ (found in an other stackoverflow question) it work's well, but if I have three calls he ask me three times to execute the command with admin privileges..and this isn't very user friendly.I tried also to use "runas":runas /noprofile /user:administrator netsh ......But in this case the problem is that: the administrator user have to be active otherwise I have to found a way to scan all the active users with administration permissions. In addiction after the runas I must interact with prompt and write the password.2) There is a way to scan all the wifi interfaces for the last netsh command? 解决方案 I was researching a little bit about elevate tool and I made some tests. Here's the key to make it work (from the documentation):Usage: Elevate [-?|-wait|-k] prog [args]-k - Starts the the %comspec% environment variable value and executes prog in it (CMD.EXE, 4NT.EXE, etc.)prog - The program to executeI downloaded the elevate.exe executable and put it in my NetBeans project folder. Then I create a single process for each netsh command you want to run (I made a little modifications but only for testing purposes).Disclaimer: Tested on Windows 7 Home, not Windows 8.Finally, as @damryfbfnetsi pointed out in his comment, you should use ProcessBuilder instead of Runtime as follows:public static void main(String[] args) { try { System.out.println("-- Setting up WLAN --"); String netshCommand = "netsh wlan set hostednetwork mode=allow ssid=\"YourSSID\" key=\"YourPassword\" & exit"; String[] elevateCommand = new String[]{"./Release/elevate.exe", "-wait", "-k", "prog", netshCommand}; ProcessBuilder pb1 = new ProcessBuilder(elevateCommand); Process p1 = pb1.start(); p1.waitFor(); System.out.println("-- Starting WLAN --"); netshCommand = "netsh wlan start hostednetwork & exit"; elevateCommand = new String[]{"./Release/elevate.exe", "-wait", "-k", "prog", netshCommand}; ProcessBuilder pb2 = new ProcessBuilder(elevateCommand); Process p2 = pb2.start(); p2.waitFor(); System.out.println("-- Setting up IPv4 interface --"); netshCommand = "netsh interface ipv4 set address \"Conexión de red inalámbrica\" static 192.168.0.102 255.255.255.0 192.168.0.254 & exit"; elevateCommand = new String[]{"./Release/elevate.exe", "-wait", "-k", "prog", netshCommand}; ProcessBuilder pb3 = new ProcessBuilder(elevateCommand); Process p3 = pb3.start(); p3.waitFor(); System.out.println("-- Getting IPv4 interface dump --"); netshCommand = "netsh interface ipv4 dump"; ProcessBuilder pb4 = new ProcessBuilder("cmd.exe", "/c", netshCommand); Process p4 = pb4.start(); System.out.println("-- Printing IPv4 interface dump --"); BufferedReader bfr = new BufferedReader(new InputStreamReader(p4.getInputStream(),"ISO-8859-1")); String output; while((output = bfr.readLine()) != null){ System.out.println(output); } } catch (IOException | InterruptedException ex) { ex.printStackTrace(); }}Note: please note netshCommand ends with & exit. That's because if it doesn't then the cmd console open by elevate.exe will remain open.The output should look like this (sorry for the Spanish words but I have my Windows in that language):-- Setting up WLAN ---- Starting WLAN ---- Setting up IPv4 interface ---- Getting IPv4 interface dump ---- Printing IPv4 interface dump --#----------------------------------# Configuración de IPv4#----------------------------------pushd interface ipv4reset set global icmpredirects=enabledadd route prefix=0.0.0.0/0interface="Conexión de red inalámbrica" nexthop=192.168.0.254 metric=1 publish=Sí add address name="Conexión de red inalámbrica" address=192.168.0.102 mask=255.255.255.0popd# Fin de la configuración de IPv4 这篇关于Java在Windows 8中创建WiFi热点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-18 06:29