۴- حالا باید کلاس JavaLauncher بسازیم
package runjar;
/**
*
* @author kian
*/
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
/**
* JavaLauncher Provides an easy way to launch java appliations. The
ClasspathBuilder
class
* is handle to use in conjunction with this class.
* @author Jason Ederle
*/
public class JavaLauncher {
private static boolean debug = false;
/**
* Launch a java program.
* @param mainClass - class with main method
* @param classpath - the java classpath
* @param jvmargs - arguments for the jvm
* @param properties - any system properties
* @param xDockName - Mac os x, application name
* @param xDockIcon - Mac os x, application icon
*/
public static Process exec(String mainClass, String classpath, String[] jvmargs, String[] properties, String xDockName) throws Exception {
//get a jvm to execute with
String jvm = findJVM();
StringBuffer strClasspath = new StringBuffer("." + File.pathSeparator + classpath);
//combine all the arguments into 1 array.
String[] allArguments = new String[properties.length + jvmargs.length];
System.arraycopy(jvmargs, 0, allArguments, 0, jvmargs.length);
System.arraycopy(properties, 0, allArguments, jvmargs.length, properties.length);
//build the command with jvm, arguments, and mainclass
String[] command = new String[5 + allArguments.length];
//put java command in place
command[0] = jvm;
//add all the arguments
System.arraycopy(allArguments, 0, command, 1, allArguments.length);
//set application name
command[allArguments.length + 1] = "-Xdock:name=" + xDockName;
command[allArguments.length + 2] = "-classpath";
command[allArguments.length + 3] = "\"" + strClasspath + "\" ";
command[allArguments.length + 4] = mainClass;
String[] env = {};
//combine to printable string for debugging
StringBuffer wholeCommand = new StringBuffer();
for (int i = 0; i < proc =" Runtime.getRuntime().exec(command);" jvm =" findJVM();" comm =" new" proc =" Runtime.getRuntime().exec(comm);" inputstream =" proc.getErrorStream();" inputstreamreader =" new" bufferedreader =" new" line =" bufferedreader.readLine())" value = " + proc.exitValue()); } } catch (InterruptedException e) { System.out.println(" jvm =" null;" jvm =" System.getProperty(" jvm ="=" jvm = "java" jvm =" jvm" style="text-align: right;">
۵- کد های زیر را به کلاس RunJarView اضافه کنید.و سپس پکیج هایی رو که نت بینز هشدار میده import کنید.
public URL getResourcePath(String yourPath) {
String mainPath = null;
String mainElement = null;
URL mainURL = null;
if (System.getProperty("os.name").equalsIgnoreCase("linux")) {
try {
URL inputURL = getClass().getResource("");
if (inputURL.toString().contains("!")) {
File file = new File(getClass().getResource("").getFile());
inputURL = new URL("file", "", file.getParent());
// JOptionPane.showMessageDialog(null, "1 " + inputURL.toString());
mainElement = inputURL.toString().replace("file:file:", "");
// JOptionPane.showMessageDialog(null, "2 " + mainElement);
String element = URLDecoder.decode(mainElement.substring(0, mainElement.lastIndexOf(File.separator)) + File.separator, "utf-8");
// JOptionPane.showMessageDialog(null, "3 " + element);
mainPath = element.concat(yourPath);
// JOptionPane.showMessageDialog(null, "4 " + mainPath);
mainURL = new URL("jar", "", mainPath);
} else {
String[] strArray = yourPath.split(File.separator);
mainURL = new URL("jar", "", System.getProperty("user.dir") + File.separator + strArray[0] + File.separator + strArray[1]);
}
} catch (Exception ex) {
}
} else {
try {
URL inputURL = getClass().getResource("");
if (inputURL.toString().contains("!")) {
File file = new File(getClass().getResource("").getFile());
inputURL = new URL("file", "", file.getParent());
// JOptionPane.showMessageDialog(null, "1 " + inputURL.toString());
mainElement = inputURL.toString().replace("file:file:" + File.separator, "");
// JOptionPane.showMessageDialog(null, "2 " + mainElement);
String element = URLDecoder.decode(mainElement.substring(0, mainElement.lastIndexOf(File.separator)) + File.separator, "utf-8");
// JOptionPane.showMessageDialog(null, "3 " + element);
mainPath = element.concat(yourPath);
// JOptionPane.showMessageDialog(null, "4 " + mainPath);
mainURL = new URL("jar", "", mainPath);
} else {
String[] strArray = yourPath.split("\\" + File.separator);
mainURL = new URL("jar", "", System.getProperty("user.dir") + File.separator + strArray[0] + File.separator + strArray[1]);
}
} catch (Exception ex) {
}
}
return mainURL;
}public URL getResourcePath(String yourPath) {
String mainPath = null;
String mainElement = null;
URL mainURL = null;
if (System.getProperty("os.name").equalsIgnoreCase("linux")) {
try {
URL inputURL = getClass().getResource("");
if (inputURL.toString().contains("!")) {
File file = new File(getClass().getResource("").getFile());
inputURL = new URL("file", "", file.getParent());
// JOptionPane.showMessageDialog(null, "1 " + inputURL.toString());
mainElement = inputURL.toString().replace("file:file:", "");
// JOptionPane.showMessageDialog(null, "2 " + mainElement);
String element = URLDecoder.decode(mainElement.substring(0, mainElement.lastIndexOf(File.separator)) + File.separator, "utf-8");
// JOptionPane.showMessageDialog(null, "3 " + element);
mainPath = element.concat(yourPath);
// JOptionPane.showMessageDialog(null, "4 " + mainPath);
mainURL = new URL("jar", "", mainPath);
} else {
String[] strArray = yourPath.split(File.separator);
mainURL = new URL("jar", "", System.getProperty("user.dir") + File.separator + strArray[0] + File.separator + strArray[1]);
}
} catch (Exception ex) {
}
} else {
try {
URL inputURL = getClass().getResource("");
if (inputURL.toString().contains("!")) {
File file = new File(getClass().getResource("").getFile());
inputURL = new URL("file", "", file.getParent());
// JOptionPane.showMessageDialog(null, "1 " + inputURL.toString());
mainElement = inputURL.toString().replace("file:file:" + File.separator, "");
// JOptionPane.showMessageDialog(null, "2 " + mainElement);
String element = URLDecoder.decode(mainElement.substring(0, mainElement.lastIndexOf(File.separator)) + File.separator, "utf-8");
// JOptionPane.showMessageDialog(null, "3 " + element);
mainPath = element.concat(yourPath);
// JOptionPane.showMessageDialog(null, "4 " + mainPath);
mainURL = new URL("jar", "", mainPath);
} else {
String[] strArray = yourPath.split("\\" + File.separator);
mainURL = new URL("jar", "", System.getProperty("user.dir") + File.separator + strArray[0] + File.separator + strArray[1]);
}
} catch (Exception ex) {
}
}
return mainURL;
}public URL getResourcePath(String yourPath) {
String mainPath = null;
String mainElement = null;
URL mainURL = null;
if (System.getProperty("os.name").equalsIgnoreCase("linux")) {
try {
URL inputURL = getClass().getResource("");
if (inputURL.toString().contains("!")) {
File file = new File(getClass().getResource("").getFile());
inputURL = new URL("file", "", file.getParent());
// JOptionPane.showMessageDialog(null, "1 " + inputURL.toString());
mainElement = inputURL.toString().replace("file:file:", "");
// JOptionPane.showMessageDialog(null, "2 " + mainElement);
String element = URLDecoder.decode(mainElement.substring(0, mainElement.lastIndexOf(File.separator)) + File.separator, "utf-8");
// JOptionPane.showMessageDialog(null, "3 " + element);
mainPath = element.concat(yourPath);
// JOptionPane.showMessageDialog(null, "4 " + mainPath);
mainURL = new URL("jar", "", mainPath);
} else {
String[] strArray = yourPath.split(File.separator);
mainURL = new URL("jar", "", System.getProperty("user.dir") + File.separator + strArray[0] + File.separator + strArray[1]);
}
} catch (Exception ex) {
}
} else {
try {
URL inputURL = getClass().getResource("");
if (inputURL.toString().contains("!")) {
File file = new File(getClass().getResource("").getFile());
inputURL = new URL("file", "", file.getParent());
// JOptionPane.showMessageDialog(null, "1 " + inputURL.toString());
mainElement = inputURL.toString().replace("file:file:" + File.separator, "");
// JOptionPane.showMessageDialog(null, "2 " + mainElement);
String element = URLDecoder.decode(mainElement.substring(0, mainElement.lastIndexOf(File.separator)) + File.separator, "utf-8");
// JOptionPane.showMessageDialog(null, "3 " + element);
mainPath = element.concat(yourPath);
// JOptionPane.showMessageDialog(null, "4 " + mainPath);
mainURL = new URL("jar", "", mainPath);
} else {
String[] strArray = yourPath.split("\\" + File.separator);
mainURL = new URL("jar", "", System.getProperty("user.dir") + File.separator + strArray[0] + File.separator + strArray[1]);
}
} catch (Exception ex) {
}
}
return mainURL;
}
۰
۶- یک پوشه به نام resource در پوشه RunJar بسازید و فایل جر مورد نظر را که در اینجا به نام runtest هست را در آن قرار دهید.
۷- برنامه را کلین و بیلد کنید و سپس اجرا کنید.
با سپاس از استاد شایسته علوی