Search code examples
javaeclipsejarawtrobot

Java program works in eclipse but in from jar


I'm writing a simple java program with the intent to break an afk limit. Whenever I run it in Eclipse, it works just fine. However, when I export it as a runnable jar file, It won't simulate key presses. I believe I have the correct run configuration and everything which is why I'm so confused. Anyway, here is the main class:

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);
        }
    }

}

And here is the other class:

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;
            }
        }
    }
}

They GUI will pop up and I can click the button however no key presses are simulated. Any help would be greatly appreciated!


Solution

  • Wow sorry. Eclipse does export those dependancies but like an idiot, I forgot to add my JRE to my build path. And PS @Dileep, you don't 'have' to do it separately.