如何从Java代码打开我的电脑和控制面板



我**试图从java代码打开我的计算机和控制面板.**我尝试了堆栈溢出相关问题的所有解决方案,但没有找到合适的答案。

我还在尝试执行以下操作:-

  1. 从 java 代码中删除临时文件
  2. 通过Java代码控制笔记本电脑/PC的音量
  3. 从 Java 代码打开默认浏览器。请尽快帮助我。提前谢谢。

使用以下代码

打开控制面板

    Runtime.getRuntime().exec("cmd /c start control");

打开"我的电脑"的步骤

        Runtime.getRuntime().exec("cmd /c start explorer");

要打开浏览器

Runtime.getRuntime().exec("cmd /c start chrome.exe");

库java.awt.Robot可能会以某种方式帮助你。使用此库,您可以按下和释放键盘命中。

当然,你必须发展一点。但是你可以用按键做很多事情。

如果要打开资源管理器,则需要按windows keye

  Robot robot = new Robot();
        robot.keyPress(KeyEvent.VK_WINDOWS);
        robot.keyPress(KeyEvent.VK_E);
        robot.keyRelease(KeyEvent.VK_WINDOWS);
        robot.keyRelease(KeyEvent.VK_E);

这里有一个简单的例子:

try {
        Robot robot = new Robot();
        // Simulate a mouse click
        robot.mousePress(InputEvent.BUTTON1_MASK);
        robot.mouseRelease(InputEvent.BUTTON1_MASK);
        // Simulate a key press
        robot.keyPress(KeyEvent.VK_A);
        robot.keyRelease(KeyEvent.VK_A);
} catch (AWTException e) {
        e.printStackTrace();
}

对于第三点,您可以使用:

进口

import java.awt.Desktop;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

法典

if(Desktop.isDesktopSupported()){
    try {
        Desktop.getDesktop().browse(new URI("http://www.example.com"));
        System.out.println("Browser geöffnet");
    } catch (IOException e) {
        e.printStackTrace();
    } catch (URISyntaxException e) {
        e.printStackTrace();
    }
}

你可以使用这样的东西。使用 Java 桌面类是可能的

Desktop desktop = null;
  if (Desktop.isDesktopSupported()) {
    desktop = Desktop.getDesktop();
  }
 Desktop.getDesktop().open(new File("c:\"));

阅读文档以更深入:https://docs.oracle.com/javase/7/docs/api/java/awt/Desktop.html

另一个考虑因素可以是

import java.io.BufferedReader;import java.io.InputStreamReader;

public class ExecuteShellComand {

public static void main(String[] args) {
    ExecuteShellComand obj = new ExecuteShellComand();
    String domainName = "google.com";
    //in windows
    //String command = "control panel";
    String output = obj.executeCommand(command);
    System.out.println(output);
}
private String executeCommand(String command) {
    StringBuffer output = new StringBuffer();
    Process p;
    try {
        p = Runtime.getRuntime().exec(command);
        p.waitFor();
        BufferedReader reader = 
                        new BufferedReader(new InputStreamReader(p.getInputStream()));
                    String line = "";           
        while ((line = reader.readLine())!= null) {
            output.append(line + "n");
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return output.toString();
}

}

最新更新