發表文章

目前顯示的是 5月, 2012的文章

Run the console application at background (hide the console window)

Create a VB script with the following content: Set WshShell = CreateObject("WScript.Shell") WshShell.Run chr(34) & "Your command to run, bat or exe" & Chr(34), 0 Set WshShell = Nothing

Java Wrapper Framework

Comparison of Java Wrapper Framework Comparison of wrapper frameworks YAJSW :   Yet Another Java Service Wrapper JSW :   Java Service Wrapper ACD :   Apache Commons Daemon L4J :   Launch4j Note: If you would like to add further frameworks or in case of errors in the table please inform me. Features/Framework YAJSW JSW ACD L4J License LGPL GPL/Commercial Apache BSD/MIT Run as Windows Service X X X   Run as UNIX Daemon X X X   Support Mac OS X launchd X       Platform indepemdent installation of services/Daemons X       Java Embedable X       Wrap Java Application X X X X Wrap Groovy Script X Wrap native executable X   X   Wrap as executable       X Portable Configuration X X     Capture and Log console output X X     Stop and Reconnect Wrapper X       Monitor Application X X X   Restart Application X X X   Single Instance Enforcement X X   X Control Process Priority X X     Control Process Affinity X       Alert Emails X X (Commercial)     Scripting X    

Version string comparison in Java

1. Use maven API - DefaultArtifactVersion class import org.apache.maven.artifact.versioning.DefaultArtifactVersion; DefaultArtifactVersion minVersion = new DefaultArtifactVersion("1.0.1"); DefaultArtifactVersion maxVersion = new DefaultArtifactVersion("1.10"); DefaultArtifactVersion version = new DefaultArtifactVersion("1.11"); if (version.compareTo(minVersion) == -1 || version.compareTo(maxVersion) == 1) { System.out.println("Sorry, your version is unsupported"); } 2. Create a VersionComparator class // VersionComprator.java import java.util.Comparator; public VersionComprator implements Comparator { public boolean equals(Object o1, Object o2) { return compare(o1, o2) == 0; } public int compare(Object o1, Object o2) { String version1 = (String) o1; String version2 = (String) o2; VersionTokenizer tokenizer1 = new VersionTokenizer(version1); Versio

Shutting down windows operating system by Java

public class Main { public static void main(String[] args) throws IOException { String shutdownCommand=""; String operatingSystem = System.getProperty("os.name"); if ("Linux".equals(operatingSystem) || "Mac OS X".equals(operatingSystem)) { shutdownCommand = "shutdown -h now"; } else if (operatingSystem.startWith("Window")) { shutdownCommand = "shutdown -s -t 0"; } Runtime.getRuntime().exec(shutdownCommand); System.exit(0); } }

Event called when application is being closed

Use Runtime class to register a thread which will be called when the program is being closed.   Runtime . getRuntime (). addShutdownHook ( theHookThread );   Reference: http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Runtime.html#addShutdownHook%28java.lang.Thread%29

Implement a single instance JAVA application

1. Use file lock to ensure single instance is running for this application. private static boolean lockInstance(final String lockFile) { try { final File file = new File(lockFile); final RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw"); final FileLock fileLock = randomAccessFile.getChannel().tryLock(); if (fileLock != null) { Runtime.getRuntime().addShutdownHook(new Thread() { public void run() { try { fileLock.release(); randomAccessFile.close(); file.delete(); } catch (Exception e) { log.error("Unable to remove lock file: " + lockFile, e); } } }); return true; } } catch (Exception e) { log.error("Unable to create and/or lock file: " + lockFile, e); } return false; } 2. Another file locking example: 2a.JustOneLock.ja

Windows cannot delete file or folder

Reboot Reboot, then delete. DEL or RMDIR Open a command line window (WindowsKey + R, enter: cmd). Move to the folder in question by means of CD commands like CD \ and CD foldername. Delete the file or folder by using the DEL command to delete files or the RMDIR (remove directory) command to remove directories (folders). Kill explorer.exe If this, on its own, is still not enough, then leave the command line window on the desktop, open Task Manager (Ctrl + Shift + Esc), and kill all explorer.exe tasks. Your desktop will go blank, except for the windows already open. Now try to delete the offending file by means of the DEL command, or folder by means of the RMDIR (remove directory) command, in the command line window, as described above. After that, in the command line window enter: explorer to restart your desktop. If you closed the command line window, you can still restart explorer by opening the Task Manager by holding down the Ctrl and Shift keys and briefly p