我正在编写一个简单的java程序,旨在打破afk限制。每当我在Eclipse中运行它时,它运行得很好。但是,当我将其导出为可运行的jar文件时,它不会模拟按键。我相信我有正确的运行配置和一切,这就是为什么我很困惑。 无论如何,这里是主要的课程:
package minebot;
import javax.swing.JOptionPane;
public class MineBotRunner
{
public static void main(String[] args)
{
try
{
MineBot bot = new MineBot();
bot.run();
}
catch (Exception e)
{
e.printStackTrace();
JOptionPane.showMessageDialog(null, "MineBot encountered an error and will now close.", "MineBot", -1);
System.exit(0);
}
}
}
这是另一个班级:
package minebot;
import java.awt.AWTException;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Robot;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class MineBot implements ActionListener
{
private JFrame frame;
private JButton button;
private String title = "MineBot";
private boolean isMoving = false;
private long sysTime;
private long waitTime;
private long delay = 780000; //A 13 minute interval between actions to beat the 15 minute AFK limit.
private int holdDelay = 500; //A .5 second hold time for key presses.
private int wait = 200; //A .2 second wait time between forward and reverse actions.
private int forward = KeyEvent.VK_W; //The key held for the first action.
private int backward = KeyEvent.VK_S; //The key held for the second action.
private Robot robo;
public MineBot()
{
}
private void CreateGUI()
{
try
{
robo = new Robot();
}
catch (AWTException e)
{
JOptionPane.showMessageDialog(null, "MineBot encountered an error and will now close.",title, 2);
System.exit(0);
}
frame = new JFrame(title);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setPreferredSize(new Dimension(250,150));
button = new JButton("Start");
button.setBackground(Color.RED);
button.addActionListener(this);
button.setActionCommand("button");
frame.add(button);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
JOptionPane.showMessageDialog(frame, "<HTML><CENTER>Welcome to MineBot! If you have any ideas<br> for additional features or programs, please<br> email me at <a color=#fff>Zanda268@gmail.com</a>!</CENTER</HTML>",title, -1);
}
private void StartMoveLoop() throws InterruptedException
{
sysTime = System.currentTimeMillis();
waitTime = sysTime + delay;
while(true)
{
if(isMoving)
{
sysTime = System.currentTimeMillis();
if(sysTime>waitTime)
{
waitTime = sysTime + delay;
robo.keyPress(forward);
Thread.sleep(holdDelay);
robo.keyRelease(forward);
Thread.sleep(wait);
robo.keyPress(backward);
Thread.sleep(holdDelay);
robo.keyRelease(backward);
}
}
}
}
public void run() throws InterruptedException
{
CreateGUI();
StartMoveLoop();
}
public void actionPerformed(ActionEvent a)
{
if(a.getActionCommand().equals("button"))
{
if(isMoving)
{
button.setBackground(Color.RED);
button.setText("Start");
isMoving = false;
}
else
{
button.setBackground(Color.GREEN);
button.setText("Stop");
isMoving = true;
}
}
}
}
他们会弹出GUI,我可以点击按钮,但不会模拟按键。任何帮助将不胜感激!
答案 0 :(得分:3)
Eclipse包含很多Jar文件,它使用jar来执行项目。
但是当你将它转换为可运行的Jar时。它不会扭曲所有的 包里面。您必须将它们分别添加到项目库中。 否则会导致运行Jar的问题。!!使用“添加构建路径”添加依赖项。该 编译Java项目时使用Java构建路径来发现依赖类和 这就是JVM识别依赖类的方式。
在大型项目中,您可以使用Class Dependency Analyzer来查找项目中的依赖项。
答案 1 :(得分:-3)
哇抱歉。 Eclipse确实导出了这些依赖项,但就像一个白痴,我忘了将我的JRE添加到我的构建路径中。和PS @Dileep一样,你没有“分开”。