Jump to content

Copy Jar File To Scripts Folder


liverare

Recommended Posts

You can make your jar files copy themselves across to the OSBot script directory. This is mostly useful if you plan on giving someone your script in the form of a jar file, and you can try it out yourself with my Glassblowing script.

 

F3XDNzQ.png

 

1SMPTeT.png

 

Note:

  • Nothing will happen if you try to run the jar file from within your script folder.
  • The jar cannot delete itself. This is possible launching a batch process, but that's hacky and more effort than it's worth.

 

When you export your project to a jar, make the code below your main class:

Save as MoveToOSBotScriptFolder.java

import java.awt.Desktop;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

/**
 * Move Jar file from wherever it currently is to OSBot's script folder.
 * 
 * @author LiveRare
 *
 */
public class MoveToOSBotScriptFolder {

	public static final String OSBOT_SCRIPT_DIR = System.getProperty("user.home")
			+ File.separatorChar
			+ "osbot"
			+ File.separatorChar
			+ "scripts"
			+ File.separatorChar;
	
	public static final File OSBOT_SCRIPT_FILE = new File(OSBOT_SCRIPT_DIR);

	public static final String THIS_FILE_DIR = MoveToOSBotScriptFolder.class.getProtectionDomain()
			.getCodeSource().getLocation().getFile().replaceAll("%20", " ");
	
	public static final File THIS_JAR_FILE = new File(THIS_FILE_DIR);

	public static final File OSBOT_SCRIPT_JAR_FILE = new File(OSBOT_SCRIPT_FILE, THIS_JAR_FILE.getName());

	public static void main(String[] args) {
		
		try {
			
			if (!isRunningJarAlreadyInScriptsFolder()) {
				
				setSystemLAF();
				
				if (alreadyExists() ? requestPermissionToOverwriteFile() : requestPermissionToMoveFile()) {
					
					if (deleteJarFileInScriptFolder() && createJarFileInScriptFolder()) {
						
						copyFileAcross();
						
						openOSBotScriptFolder();
					}
				}
			}
			
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	private static boolean isRunningJarAlreadyInScriptsFolder() {
		return THIS_JAR_FILE.equals(OSBOT_SCRIPT_JAR_FILE);
	}
	
	private static void setSystemLAF() {
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (ClassNotFoundException | InstantiationException | IllegalAccessException
				| UnsupportedLookAndFeelException e) {
			e.printStackTrace();
		}
	}

	private static boolean requestPermissionToMoveFile() {
		return JOptionPane.showConfirmDialog(null,
				String.format("Do you want to move:\n\n%s\n\nto\n\n%s", THIS_JAR_FILE, OSBOT_SCRIPT_FILE), "Confirm",
				JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION;
	}
	
	private static boolean requestPermissionToOverwriteFile() {
		return JOptionPane.showConfirmDialog(null,
				String.format("%s already exists.\nDo you want to replace it?", OSBOT_SCRIPT_JAR_FILE.getName()), "Confirm",
				JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION;
	}
	
	private static boolean alreadyExists() {
		return OSBOT_SCRIPT_JAR_FILE.exists() && OSBOT_SCRIPT_JAR_FILE.isFile();
	}

	private static void copyFileAcross() throws FileNotFoundException, IOException {
		
		byte[] buffer = new byte[1024];
		int length;
		
		try (
				FileInputStream in = new FileInputStream(THIS_JAR_FILE);
				FileOutputStream out = new FileOutputStream(OSBOT_SCRIPT_JAR_FILE);
			) {
			
			while ((length = in.read(buffer)) > 0) {

				out.write(buffer, 0, length);

			}
		}
	}
	
	private static boolean deleteJarFileInScriptFolder() {
		return !OSBOT_SCRIPT_JAR_FILE.exists() || OSBOT_SCRIPT_JAR_FILE.delete();
	}
	
	private static boolean createJarFileInScriptFolder() throws IOException {
		return OSBOT_SCRIPT_JAR_FILE.exists() || OSBOT_SCRIPT_JAR_FILE.createNewFile();
	}
	
	private static void openOSBotScriptFolder() {
		try {
			if (Desktop.isDesktopSupported()) {
				Desktop.getDesktop().browse(OSBOT_SCRIPT_FILE.toURI());
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

 

Edited by liverare
Something happened when editing thread last time. o_o
  • Like 2
Link to comment
Share on other sites

5 minutes ago, TheWind said:

I just save all my scripts to the script folder and it overwrites them through eclipse, but nonetheless a nice snippet.

I only compile a jar file when I intend to distribute a script. Otherwise, my Eclipse will compile my script and save the compiled files to OSBot's script folder too. :)

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...