2010-06-01 10 views
6

İşte neyle geldiğini. Çalışıyor ama daha zarif bir şey olup olmadığını merak ediyordum. Teşekkür ederim!Java programımı Mac OS/Windows'ta Sistem Başlangıcı üzerinde çalıştırmak istiyorum. Bunu nasıl yapabilirim?

Misha

/* Copyright (c) 2010 Misha Koshelev. All Rights Reserved. 
* 
* TODO: 
* - Add Linux support 
*/ 
package com.mksoft.common; 

import java.io.BufferedReader; 
import java.io.File; 
import java.io.FileWriter; 
import java.io.InputStreamReader; 
import java.io.IOException; 
import java.io.PrintWriter; 

import java.net.URI; 
import java.net.URISyntaxException; 

/** 
* Run specified class within this JAR file on system startup. 
* 
* @author Misha Koshelev 
*/ 
public class RunOnSystemStartup {  
    /* 
    * Constants 
    */ 
    protected final static String osName=System.getProperty("os.name"); 
    protected final static String fileSeparator=System.getProperty("file.separator"); 
    protected final static String javaHome=System.getProperty("java.home"); 
    protected final static String userHome=System.getProperty("user.home"); 

    /* 
    * Debugging 
    */ 
    protected static boolean debugOutput=false; 
    protected static void debug(String message) { 
    if (debugOutput) { 
     System.err.println(message); 
     System.err.flush(); 
    } 
    } 

    /* 
    * Helpers 
    */ 
    protected static File getJarFile() throws URISyntaxException { 
    return new File(RunOnSystemStartup.class.getProtectionDomain().getCodeSource().getLocation().toURI()); 
    } 
    protected static File getStartupFile() throws Exception { 
    debug("RunOnSystemStartup.getStartupFile: osName=\""+osName+"\""); 
    if (osName.startsWith("Windows")) { 
     Process process=Runtime.getRuntime().exec("reg query \"HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders\" /v Startup"); 
     BufferedReader in=new BufferedReader(new InputStreamReader(process.getInputStream())); 
     String result="",line; 
     while ((line=in.readLine())!=null) { 
     result+=line; 
     } 
     in.close(); 
     result=result.replaceAll(".*REG_SZ[ ]*",""); 
     debug("RunOnSystemStartup.getStartupFile: Startup Directory=\""+result+"\""); 

     return new File(result+fileSeparator+getJarFile().getName().replaceFirst(".jar",".bat")); 
    } else if (osName.startsWith("Mac OS")) { 
     return new File(userHome+"/Library/LaunchAgents/com.mksoft."+getJarFile().getName().replaceFirst(".jar",".plist")); 
    } else { 
     throw new Exception("Unknown Operating System Name \""+osName+"\""); 
    } 
    } 

    /* 
    * Methods 
    */ 

    /** 
    * Returns whether this JAR file is installed to run on system startup. 
    */ 
    public static boolean isInstalled() throws Exception { 
    return getStartupFile().exists(); 
    } 

    /** 
    * Install the specified class from the current JAR file to run on system startup. 
    * 
    * @param className Name of class within the current JAR file to run on system startup. 
    * @param windowTitle Title to display in window title bar, if applicable. 
    */ 
    public static void install(String className,String windowTitle) throws Exception { 
    File startupFile=getStartupFile(); 
    PrintWriter out=new PrintWriter(new FileWriter(startupFile)); 
    if (osName.startsWith("Windows")) { 
     out.println("@echo off"); 
     out.println("start \""+windowTitle+"\" \""+javaHome+fileSeparator+"bin"+fileSeparator+"java.exe\" -cp "+getJarFile()+" "+className); 
    } else if (osName.startsWith("Mac OS")) { 
     out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); 
     out.println("<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">"); 
     out.println("<plist version=\"1.0\">"); 
     out.println("<dict>"); 
     out.println(" <key>Label</key>"); 
     out.println(" <string>com.mksoft."+getJarFile().getName().replaceFirst(".jar","")+"</string>"); 
     out.println(" <key>ProgramArguments</key>"); 
     out.println(" <array>"); 
     out.println("  <string>"+javaHome+fileSeparator+"bin"+fileSeparator+"java</string>"); 
     out.println("  <string>-cp</string>"); 
     out.println("  <string>"+getJarFile()+"</string>"); 
     out.println("  <string>"+className+"</string>"); 
     out.println(" </array>"); 
     out.println(" <key>RunAtLoad</key>"); 
     out.println(" <true/>"); 
     out.println("</dict>"); 
     out.println("</plist>"); 
    } else { 
     throw new Exception("Unknown Operating System Name \""+osName+"\""); 
    } 
    out.close(); 
    } 

    /** 
    * Uninstall this JAR file from the system startup process. 
    */ 
    public static void uninstall() throws Exception { 
    File startupFile=getStartupFile(); 
    if (startupFile.exists()) { 
     startupFile.delete(); 
    } 
    } 
} 
+0

Kodunuzu seviyorum – Simmant

cevap

0

Eğer (geçerli kullanıcı için ya da bunu çalıştırmak istediğinizde bağlı Tüm Kullanıcılar için ya), Windows için başlangıçta çalıştırmak olacaktır Başlat menüsündeki Başlangıç ​​klasörüne kısayolları oluşturun sen.

0

Bu mücevher için teşekkürler, son geliştirmemde kullandım.

Benim yaklaşımımda kullandığım tek gelişme, plist'in inşasıdır. Pakete bir şablon XML'si ekliyorum. Daha sonra XML'de okumak için bir sınıf yükleyici kullanıyorum ve daha sonra yukarıda yaptığınız gibi bir Dosya kullanarak yazdım.

orijinal yazı ile Yardımlarınız için

private String loadPlistXML() { 
    StringBuilder file = new StringBuilder(); 
    try{ 
     is = this.getClass().getClassLoader().getResourceAsStream("LaunchPList.xml"); 
     logger.info("is = " + (is != null)); 
     BufferedReader in = new BufferedReader(new InputStreamReader(is)); 
     String line = null; 
     while((line = in.readLine()) != null) { 
      file.append(line +"\n"); 
     } 

    }catch(Error e){ 
     e.printStackTrace(); 
    }catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    }  
    return file.toString(); 
} 

tekrar teşekkürler (bir gelişme Az ve daha bir derli toplu yukarı aşağıya bakınız).

İlgili konular