2024-03-19 15:55:51 | 我爱编程网
以下是在win7系统中如何让我们自己写的java程序开机自启
1,首先我们需要把我们的java程序打成可以运行的jar,放到当前系统的 classpath 目录中。
2,新建 bat 文件。流程如下:
@echo off
java -jar 路径\xxxx.jar
@pause
然后点击文件-另存为(存放jar的目录下),文件名修改xxxx.bat(提议命名和jar名一致)。
3,windows+r输入regedit会出现注册表编辑器。
4,在注册表编辑器中找到Run目录,具体路径如下:
HKEY_LOCAL_MACHINE\SOFTWARE\Mecrosoft\Windows\CurrentVersion\Run
5,找到Run目录之后,在右边框中右击新建字符串值,新建唯一标示作为键名,然后右击新建名称-修改在数值数据中填写"路径\xxxx.bat"。
6,重启你的机器,你会看到你想要的效果。
使用批处理即可。。。
如下:
public static void autorun(boolean shouldAdd)我爱编程网
{
Process process;
try
{
process = Runtime.getRuntime().exec("cmd");
SequenceInputStream sis = new SequenceInputStream(process.getInputStream(), process.getErrorStream());
InputStreamReader isr = new InputStreamReader(sis, "GBK");
BufferedReader br = new BufferedReader(isr);
OutputStreamWriter osw = new OutputStreamWriter(process.getOutputStream());
BufferedWriter bw = new BufferedWriter(osw);
String[] commands = {
"REG ADD HKLM\\Software\\Microsoft\\Windows\\CurrentVersion\\Run /v LGDream /t REG_SZ /d \"" + System.getProperty("user.dir") +
File.separator + "李公解梦" + ".exe\"",
"REG DELETE HKLM\\Software\\Microsoft\\Windows\\CurrentVersion\\Run /v LGDream /f" };
if (shouldAdd)
{
bw.write(commands[0]);
}
else
{
bw.write(commands[1]);
}
bw.newLine();
osw.flush();
bw.flush();
bw.close();
osw.close();
String line = null;
while ((line = br.readLine()) != null)
{
System.out.println(line);
}
br.close();
isr.close();
sis.close();
process.destroy();
}
catch (IOException e)
{
e.printStackTrace();
}
}
开机自启动
2个方式
1 是修改注册表
2 是复制到启动里
方法2 是拷贝文件即可
方法1 Runtime.getRuntime().exec 执行去修改注册表
String regKey = "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run";
String myAppName = "zzzApp";
String exePath = "C:\\sss\\zzz.exe";//开机启动程序本地目录
Runtime.getRuntime().exec("reg "+(isStartAtLogon?"add ":"delete ")+regKey+" /v "+myAppName+(isStartAtLogon?" /t reg_sz /d "+exePath:" /f"));
2024-03-02 02:58:47
2024-01-01 09:58:48
2024-01-26 10:37:53
2024-03-04 07:43:42
2024-01-17 23:29:33
2024-01-01 04:24:22