Search the Community
Showing results for tags 'copy'.
-
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. 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(); } } }
- 4 replies
-
- 2
-
- jar file
- script folder
-
(and 1 more)
Tagged with: