网上找到一个放锁屏的小程序,(按文章中直接将代码贴到一个类里,打jar包,两行脚本运行jar包即可,但也有一些小曲折)mark一下
java代码的原文地址:https://www.cnblogs.com/itfeng813/p/11579360.html
package main; import java.awt.AWTException; import java.awt.Dimension; import java.awt.MouseInfo; import java.awt.Point; import java.awt.PointerInfo; import java.awt.Robot; import java.awt.Toolkit; public class Main { public static void main(String[] args) { Robot robot = null; try { robot = new Robot(); } catch (AWTException e1) { e1.printStackTrace(); } Point pos = MouseInfo.getPointerInfo().getLocation(); int last_x = pos.x; int last_y = pos.y; int mov = 1; Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); System.out.println("Screen size: " + screenSize.getWidth() + "*" + screenSize.getHeight()); while (true) { System.out.println(pos.x + " " + pos.y); PointerInfo pos_info = MouseInfo.getPointerInfo(); if (pos_info == null) { System.out.println("Get location fail!"); try { Thread.sleep(30000L); } catch (InterruptedException e) { e.printStackTrace(); } } else { pos = pos_info.getLocation(); if ((pos.x == last_x) && (pos.y == last_y)) { System.out.println("moving!"); if (pos.y <= 0) { mov = 1; } if (pos.y > 0) { mov = -1; } robot.mouseMove(pos.x, pos.y + mov); robot.mouseMove(pos.x, pos.y); } pos_info = MouseInfo.getPointerInfo(); if (pos_info == null) { System.out.println("Get location fail!"); try { Thread.sleep(30000L); } catch (InterruptedException e) { e.printStackTrace(); } } else { pos = pos_info.getLocation(); last_x = pos.x; last_y = pos.y; try { Thread.sleep(30000L); } catch (InterruptedException e) { e.printStackTrace(); } } } } } }
将这个Main类打成jar包,此处jar包名为MouseMove.jar;与jar包同目录位置写个.bat类型文件,文件内容如下:
@echo off java -jar MouseMove.jar
双击执行即可。
有几点题外话(说明):
1.关于使用IDEA将java打包成jar和exe的方法(IDEA的Artifacts打成jar包;exe4j工具转exe)
参考链接:https://blog.csdn.net/weixin_38310965/article/details/80392767
2.IDEA的Artifacts (即编译后的Java类,Web资源等的整合,用以测试、部署等工作。再白话一点,就是说某个module要如何打包,例如war exploded、war、jar、ear等等这种打包形式。某个module有了 Artifacts 就可以部署到应用服务器中了。)
详解见:https://blog.csdn.net/it_manman/article/details/79699455
3.关于@echo off 的作用(@echo off表示执行了这条命令后关闭所有命令(包括本身这条命令)的回显。)
详解见:https://blog.csdn.net/fly_as_tadpole/article/details/85177379
两个小插曲(注意:打成jar运行时遇到报错):
1. 生成的jar运行报错,Error: Invalid or corrupt jarfile
(原因:我的/META_INF目录自动生成在src/main/java/下,通过对比发现jar包里面根本没有META_INF文件夹,这就是jar无法运行的根本原因;)
(解决方案:将/META_INF目录放到 src/main/resources下)
2. 生成的jar在IDEA运行正常,但java -jar运行报错,Exception in thread "main" java.lang.UnsupportedClassVersionError
(原因:是idea打包时的jdk与电脑java的版本不一致(听起来有点难理解,绕;但问题出来才发现idea的版本是可以选择导致的)
详细见:https://blog.csdn.net/qq_16633405/article/details/75330857