[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Newsgroup Home]
|
[news.eclipse.platform.swt] Re: Running SWT Outside Eclipse Instructions
|
The following is my confidential(!) code that auto detect SWT and install
automatically. Note that on Windows, JVM keeps change with new
updates are installed. Users will suddenly find your application does not
work! Don't know what cause the problem!!!! Panic!!!!
Inlcude swt inside your JAR. Create a startup class with main(),
but do not refer any SWT directly. Inside, SWT is installed or
after install, call your main SWT class method. In this way, your
program users won't p***d off!
=================================================
/**
* Checks if a JAR file with the name of the 1st argument
* is already installed into the Java Runtime running the
* application. For example, SWT package has a "swt.jar" file.
* You can check whether SWT package is installed or not as
* follows;
*
* if (!isJarInstalled("swt.jar")) {
* // install SWT here!
* }
*
* param jarfilename a jar archive file name..
*/
static public boolean isJarInstalled(String jarfilename) {
String extdirs = System.getProperty("java.ext.dirs");
File f = new File(extdirs, jarfilename);
return f.exists();
}
/**
* This install a Jar file into the JVM's jar directory.
* Jar files can be JDBC drivers and SWT package or your own
* packages! This assumes that your JAR file containing a
* class definition specified at the first parameter
* the JAR file to install to JVM directory!
*
* installJarFile(YourApp.class, "windows/swt.jar");
*
* param c a class name in the resource jar file containing the jar to add.
* param jarpathname a jar archive path name.
*/
static public void installJarFile(Class c, String jarpathname) {
ClassLoader cl = c.getClassLoader();
String extdirs = System.getProperty("java.ext.dirs");
String jarname = null;
int x = jarpathname.lastIndexOf('/');
if (x >= 0) {
jarname = jarpathname.substring(x+1);
} else {
jarname = jarpathname;
}
InputStream stream = null;
if (cl!=null) {
stream = cl.getResourceAsStream(jarpathname);
} else {
stream = ClassLoader.getSystemResourceAsStream(jarpathname);
}
copyFile(stream, new File(extdirs, jarname));
}
/**
* Same as the previous one except that this accept input
* file instead.
*
* param in the jar file to be added.
* param jarfilename a name to be added as.
*/
static public void installJarFile(File in, String jarfilename) {
String extdirs = System.getProperty("java.ext.dirs");
try {
InputStream stream = new FileInputStream(in);;
copyFile(stream, new File(extdirs, jarfilename));
} catch (Throwable t) {
t.printStackTrace();
}
}
/**
* This install a DLL file into the JVM's DLL directory.
* DLL files can be JDBC drivers and SWT package or your own
* native packages! This assumes that your main JAR file contains
* the DLL file to install to JVM directory!
* For example, to install SWT package file located at "windows";
*
* installDllFile("windows/swt-aswt-win32-3123.DLL");
* installDllFile("windows/swt-gdip-win32-3123.DLL");
* installDllFile("windows/swt-win32-3123.DLL);
*
* param c a class name in the resource jar file containing the DLL to add.
* param dllpathname a dll file path name.
*/
static public void installDllFile(Class c, String dllpathname) {
ClassLoader cl = c.getClassLoader();
String javahome = System.getProperty("java.home");
String dllhome = javahome+File.separator+"bin";
String dllname = null;
int x = dllpathname.lastIndexOf('/');
if (x >= 0) {
dllname = dllpathname.substring(x+1);
} else {
dllname = dllpathname;
}
InputStream stream = null;
if (cl!=null) {
stream = cl.getResourceAsStream(dllname);
} else {
stream = ClassLoader.getSystemResourceAsStream(dllname);
}
copyFile(stream, new File(dllhome, dllpathname));
}
/**
* Same as the previous one except that this accept input
* file instead.
*
* param in the dll file to be added.
* param dllfilename a name to be added as.
*/
static public void installDllFile(File in, String dllfilename) {
String javahome = System.getProperty("java.home");
String dllhome = javahome+File.separator+"bin";
try {
InputStream stream = new FileInputStream(in);;
copyFile(stream, new File(dllhome, dllfilename));
} catch (Throwable t) {
t.printStackTrace();
}
}
/**
* Copy files from input stream to output file.
*/
private static void copyFile(InputStream input, File output0) {
int length = 512; // buffer block size
int read_length;
long sleeping = 1;
byte[] buffer = new byte[length];
// make sure that existing files deleted on Windows!
while (true) {
if (!output0.exists()) {
break;
}
try {Thread.sleep(sleeping);} catch (Exception e) {};
sleeping *= 2;
output0.delete();
}
FileOutputStream output = null;
try {
output = new FileOutputStream(output0);
while (true) {
read_length = input.read(buffer, 0, length);
if (read_length <= 0) {
break;
}
output.write(buffer, 0, read_length);
}
} catch (Throwable e) {
e.printStackTrace();
}
try {input.close();} catch (Throwable t) {};
try {output.close();} catch (Throwable t) {};
}