[Arakhnę-Dev] [280] * Add VM launching functions.

[ Thread Index | Date Index | More arakhne.org/dev Archives ]


Revision: 280
Author:   galland
Date:     2011-09-01 19:16:53 +0200 (Thu, 01 Sep 2011)
Log Message:
-----------
* Add VM launching functions.

Modified Paths:
--------------
    trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/VMCommandLine.java

Modified: trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/VMCommandLine.java
===================================================================
--- trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/VMCommandLine.java	2011-08-31 07:36:59 UTC (rev 279)
+++ trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/VMCommandLine.java	2011-09-01 17:16:53 UTC (rev 280)
@@ -40,7 +40,7 @@
  */
 public class VMCommandLine {
 
-	private static Class<?> classToLaunch = null;
+	private static String classnameToLaunch = null;
 	private static boolean analyzed = false;
 	private static SortedMap<String,List<Object>> commandLineOptions = null;
 	private static String[] commandLineParameters = null;
@@ -75,37 +75,156 @@
 		return null;
 	}
 	
-	/** Run a new VM with the class path of the current VM.
+	/** Run a new VM with the given class path.
 	 * 
-	 * @param class_to_launch is the class to launch.
-	 * @param additional_params is the list of additional parameters
+	 * @param classToLaunch is the class to launch.
+	 * @param classpath is the class path to use.
+	 * @param additionalParams is the list of additional parameters
 	 * @return the process that is running the new virtual machine, neither <code>null</code>
+	 * @throws IOException
+	 * @since 6.2 
+	 */
+	public static Process launchVMWithClassPath(Class<?> classToLaunch, String classpath, String... additionalParams) throws IOException {
+		return launchVMWithClassPath(classToLaunch.getCanonicalName(), classpath, additionalParams);
+	}
+
+	/** Run a new VM with the given class path.
+	 * 
+	 * @param classToLaunch is the class to launch.
+	 * @param classpath is the class path to use.
+	 * @param additionalParams is the list of additional parameters
+	 * @return the process that is running the new virtual machine, neither <code>null</code>
 	 * @throws IOException 
+	 * @since 6.2
 	 */
-	public static Process launchVM(Class<?> class_to_launch, String... additional_params) throws IOException {
+	public static Process launchVMWithClassPath(String classToLaunch, String classpath, String... additionalParams) throws IOException {
 		String java_bin = getVMBinary();
 		if (java_bin==null) throw new FileNotFoundException("java"); //$NON-NLS-1$
 		long totalMemory = Runtime.getRuntime().maxMemory() / 1024;
-		String classpath = System.getProperty("java.class.path"); //$NON-NLS-1$
-		String user_dir = System.getProperty("user.dir"); //$NON-NLS-1$
-		String[] params = new String[additional_params.length+5];
-		params[0] = java_bin;
-		params[1] = "-Xmx"+totalMemory+"k"; //$NON-NLS-1$ //$NON-NLS-2$
-		params[2] = "-classpath"; //$NON-NLS-1$
-		params[3] = classpath;
-		params[4] = class_to_launch.getCanonicalName();
-		System.arraycopy(additional_params,0,params,5,additional_params.length);
+		String user_dir = FileSystem.getUserHomeDirectoryName();
+		String[] params;
+		int nParams;
+		if (classpath!=null && !"".equals(classpath)) { //$NON-NLS-1$
+			nParams = 5;
+			params = new String[additionalParams.length+nParams];
+			params[0] = java_bin;
+			params[1] = "-Xmx"+totalMemory+"k"; //$NON-NLS-1$ //$NON-NLS-2$
+			params[2] = "-classpath"; //$NON-NLS-1$
+			params[3] = classpath;
+			params[4] = classToLaunch;
+		}
+		else {
+			nParams = 3;
+			params = new String[additionalParams.length+nParams];
+			params[0] = java_bin;
+			params[1] = "-Xmx"+totalMemory+"k"; //$NON-NLS-1$ //$NON-NLS-2$
+			params[2] = classToLaunch;
+		}
+		System.arraycopy(additionalParams,0,params,nParams,additionalParams.length);
 		return Runtime.getRuntime().exec(params,null,new File(user_dir));
 	}
 
+	/** Run a new VM with the given class path.
+	 * 
+	 * @param classToLaunch is the class to launch.
+	 * @param classpath is the class path to use.
+	 * @param additionalParams is the list of additional parameters
+	 * @return the process that is running the new virtual machine, neither <code>null</code>
+	 * @throws IOException
+	 * @since 6.2 
+	 */
+	public static Process launchVMWithClassPath(Class<?> classToLaunch, File[] classpath, String... additionalParams) throws IOException {
+		return launchVMWithClassPath(classToLaunch.getCanonicalName(), classpath, additionalParams);
+	}
+
+	/** Run a new VM with the given class path.
+	 * 
+	 * @param classToLaunch is the class to launch.
+	 * @param classpath is the class path to use.
+	 * @param additionalParams is the list of additional parameters
+	 * @return the process that is running the new virtual machine, neither <code>null</code>
+	 * @throws IOException 
+	 * @since 6.2
+	 */
+	public static Process launchVMWithClassPath(String classToLaunch, File[] classpath, String... additionalParams) throws IOException {
+		StringBuffer b = new StringBuffer();
+		for(File f : classpath) {
+			if (b.length()>0) {
+				b.append(File.pathSeparator);
+			}
+			b.append(f.getAbsolutePath());
+		}
+		return launchVMWithClassPath(classToLaunch, b.toString(), additionalParams);
+	}
+
+	/** Run a jar file inside a new VM.
+	 * 
+	 * @param jarFile is the jar file to launch.
+	 * @param additionalParams is the list of additional parameters
+	 * @return the process that is running the new virtual machine, neither <code>null</code>
+	 * @throws IOException
+	 * @since 6.2 
+	 */
+	public static Process launchVMWithJar(File jarFile, String... additionalParams) throws IOException {
+		String java_bin = getVMBinary();
+		if (java_bin==null) throw new FileNotFoundException("java"); //$NON-NLS-1$
+		long totalMemory = Runtime.getRuntime().maxMemory() / 1024;
+		String user_dir = FileSystem.getUserHomeDirectoryName();
+		int nParams = 4;
+		String[] params = new String[additionalParams.length+nParams];
+			params[0] = java_bin;
+			params[1] = "-Xmx"+totalMemory+"k"; //$NON-NLS-1$ //$NON-NLS-2$
+			params[2] = "-jar"; //$NON-NLS-1$
+			params[3] = jarFile.getAbsolutePath();
+		System.arraycopy(additionalParams,0,params,nParams,additionalParams.length);
+		return Runtime.getRuntime().exec(params,null,new File(user_dir));
+	}
+
+	/** Run a new VM with the class path of the current VM.
+	 * 
+	 * @param classToLaunch is the class to launch.
+	 * @param additionalParams is the list of additional parameters
+	 * @return the process that is running the new virtual machine, neither <code>null</code>
+	 * @throws IOException 
+	 */
+	public static Process launchVM(Class<?> classToLaunch, String... additionalParams) throws IOException {
+		return launchVM(classToLaunch.getCanonicalName(), additionalParams);
+	}
+
+	/** Run a new VM with the class path of the current VM.
+	 * 
+	 * @param classToLaunch is the class to launch.
+	 * @param additionalParams is the list of additional parameters
+	 * @return the process that is running the new virtual machine, neither <code>null</code>
+	 * @throws IOException 
+	 * @since 6.2
+	 */
+	public static Process launchVM(String classToLaunch, String... additionalParams) throws IOException {
+		return launchVMWithClassPath(
+				classToLaunch,
+				System.getProperty("java.class.path"), //$NON-NLS-1$
+				additionalParams);
+	}
+
 	/** Save parameters that permit to relaunch a VM with
 	 * {@link #relaunchVM()}.
 	 * 
-	 * @param class_to_launch is the class which contains a <code>main</code>.
+	 * @param classToLaunch is the class which contains a <code>main</code>.
 	 * @param parameters is the parameters to pass to the <code>main</code>.
 	 */
-	public static void saveVMParameters(Class<?> class_to_launch, String... parameters) {
-		classToLaunch = class_to_launch;
+	public static void saveVMParameters(Class<?> classToLaunch, String... parameters) {
+		saveVMParameters(classToLaunch.getCanonicalName(), parameters);
+	}
+
+	/** Save parameters that permit to relaunch a VM with
+	 * {@link #relaunchVM()}.
+	 * 
+	 * @param classToLaunch is the class which contains a <code>main</code>.
+	 * @param parameters is the parameters to pass to the <code>main</code>.
+	 * @since 6.2
+	 */
+	public static void saveVMParameters(String classToLaunch, String... parameters) {
+		classnameToLaunch = classToLaunch;
 		commandLineParameters = parameters;
 		if (commandLineOptions!=null) commandLineOptions.clear();
 		commandLineOptions = null;
@@ -115,12 +234,23 @@
 	/** Save parameters that permit to relaunch a VM with
 	 * {@link #relaunchVM()}.
 	 * 
-	 * @param class_to_launch is the class which contains a <code>main</code>.
+	 * @param classToLaunch is the class which contains a <code>main</code>.
 	 * @param parameters is the parameters to pass to the <code>main</code>.
 	 */
-	public static void saveVMParametersIfNotSet(Class<?> class_to_launch, String... parameters) {
-		if (classToLaunch==null) {
-			saveVMParameters(class_to_launch, parameters);
+	public static void saveVMParametersIfNotSet(Class<?> classToLaunch, String... parameters) {
+		saveVMParametersIfNotSet(classToLaunch.getCanonicalName(), parameters);
+	}
+
+	/** Save parameters that permit to relaunch a VM with
+	 * {@link #relaunchVM()}.
+	 * 
+	 * @param classToLaunch is the class which contains a <code>main</code>.
+	 * @param parameters is the parameters to pass to the <code>main</code>.
+	 * @since 6.2
+	 */
+	public static void saveVMParametersIfNotSet(String classToLaunch, String... parameters) {
+		if (classnameToLaunch==null || "".equals(classnameToLaunch)) { //$NON-NLS-1$
+			saveVMParameters(classToLaunch, parameters);
 		}
 	}
 
@@ -131,8 +261,8 @@
 	 * @throws IOException 
 	 */
 	public static Process relaunchVM() throws IOException {
-		if (classToLaunch==null) return null;		
-		return launchVM(classToLaunch, getAllCommandLineParameters());
+		if (classnameToLaunch==null) return null;		
+		return launchVM(classnameToLaunch, getAllCommandLineParameters());
 	}
 
 	/** Replies the command line including the options and the standard parameters.
@@ -559,36 +689,63 @@
 	/**
 	 * Create a interface to the command line options.
 	 * 
-	 * @param class_to_launch is the class which contains a <code>main</code>.
+	 * @param classToLaunch is the class which contains a <code>main</code>.
 	 * @param parameters is the parameters to pass to the <code>main</code>.
 	 * @see #saveVMParametersIfNotSet(Class, String[])
 	 */
-	public VMCommandLine(Class<?> class_to_launch, String... parameters) {
-		saveVMParametersIfNotSet(class_to_launch, parameters);
+	public VMCommandLine(Class<?> classToLaunch, String... parameters) {
+		saveVMParametersIfNotSet(classToLaunch, parameters);
 	}
 	
 	/**
 	 * Create a interface to the command line options.
 	 * 
-	 * @param class_to_launch is the class which contains a <code>main</code>.
+	 * @param classToLaunch is the class which contains a <code>main</code>.
+	 * @param parameters is the parameters to pass to the <code>main</code>.
+	 * @see #saveVMParametersIfNotSet(String, String[])
+	 * @since 6.2
+	 */
+	public VMCommandLine(String classToLaunch, String... parameters) {
+		saveVMParametersIfNotSet(classToLaunch, parameters);
+	}
+
+	/**
+	 * Create a interface to the command line options.
+	 * 
+	 * @param classToLaunch is the class which contains a <code>main</code>.
 	 * @param optionDefinitions is the list of definitions of the available command line options.
 	 * @param parameters is the parameters to pass to the <code>main</code>.
 	 * @see #saveVMParametersIfNotSet(Class, String[])
 	 * @see #splitOptionsAndParameters(String[])
 	 */
-	public VMCommandLine(Class<?> class_to_launch, String[] optionDefinitions, String... parameters) {
-		saveVMParametersIfNotSet(class_to_launch, parameters);
+	public VMCommandLine(Class<?> classToLaunch, String[] optionDefinitions, String... parameters) {
+		saveVMParametersIfNotSet(classToLaunch, parameters);
 		splitOptionsAndParameters(optionDefinitions);
 	}
 
 	/**
 	 * Create a interface to the command line options.
 	 * 
+	 * @param classToLaunch is the class which contains a <code>main</code>.
+	 * @param optionDefinitions is the list of definitions of the available command line options.
+	 * @param parameters is the parameters to pass to the <code>main</code>.
+	 * @see #saveVMParametersIfNotSet(String, String[])
+	 * @see #splitOptionsAndParameters(String[])
+	 * @since 6.2
+	 */
+	public VMCommandLine(String classToLaunch, String[] optionDefinitions, String... parameters) {
+		saveVMParametersIfNotSet(classToLaunch, parameters);
+		splitOptionsAndParameters(optionDefinitions);
+	}
+
+	/**
+	 * Create a interface to the command line options.
+	 * 
 	 * @see #VMCommandLine(Class, String[], String[])
 	 * @see #VMCommandLine(Class, String[])
 	 */
 	public VMCommandLine() {
-		if (classToLaunch==null) {
+		if (classnameToLaunch==null) {
 			throw new IllegalArgumentException("you must call the other constructor previously"); //$NON-NLS-1$
 		}
 	}


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/