Setting classpath programmatically [message #164842] |
Wed, 16 June 2004 21:35  |
Eclipse User |
|
|
|
Originally posted by: dtwchiu.hotmail.com
Hi,
I am trying to programmatically create a launch configuration with my own
classpath.
Here is what I am doing:
List myClassPath = new ArrayList();
myClassPath.add( "/j/foo.jar" );
myClassPath.add( "/j/bar.jar" );
ILaunchManager manager = DebugPlugin.getDefault().getLaunchManager();
ILaunchConfigurationType type =
manager.getLaunchConfigurationType(IJavaLaunchConfigurationC onstants.ID_JAVA_APPLICATION);
ILaunchConfigurationWorkingCopy wc = type.newInstance(null,
"SampleConfig");
wc.setAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJE CT_NAME,
"myJavaProject");
wc.setAttribute(IJavaLaunchConfigurationConstants.ATTR_MAIN_ TYPE_NAME,
"myClass");
wc.setAttribute(IJavaLaunchConfigurationConstants.ATTR_DEFAU LT_CLASSPATH,
false );
wc.setAttribute(IJavaLaunchConfigurationConstants.ATTR_CLASS PATH,
myClassPath );
ILaunchConfiguration config = wc.doSave();
config.launch(ILaunchManager.RUN_MODE, null);
With this, I got parsing errors when launching the application. After
some investigation, it seems whatever strings I have in the myClassPath
list, they will be written directly to the .launch file. And I look at
some other .launch file I created manaually with the eclipse UI, the
classpath is an xml segment with something like:
<?xml version="1.0" encoding="UTF-8"><runtimeClasspathEntry type="2"
path="2" path="/j/foo.bar"/>
Are we supposed to construct the xml ourselves? If not, how do we go
about setting the classpath properly?
Thanks.
David.
|
|
|
|
|
Re: Setting classpath programmatically [message #165014 is a reply to message #164975] |
Thu, 17 June 2004 16:39  |
Eclipse User |
|
|
|
Originally posted by: smesh.openrules.com
Sorry misleading you, David
Use ATTR_CLASSPATH as follows:
- setAttribute() - List filled with IRuntimeClasspathEntry
- getAttribute() - List of Strings
Construct IRuntimeClasspathEntry from the String using:
JavaRuntime.newRuntimeClasspathEntry(String);
PS. Somebody definitely had screwed this piece design. :)
Please see the code below \|/
IJavaLaunchConfigurationConstants.java
> /**
> * Launch configuration attribute key. The attribute value is an ordered list of strings
> * which are mementos for runtime class path entries. When unspecified, a default
> * classpath is generated by the classpath provider associated with a launch
> * configuration (via the <code>ATTR_CLASSPATH_PROVIDER</code> attribute).
> */
> public static final String ATTR_CLASSPATH = LaunchingPlugin.getUniqueIdentifier() + ".CLASSPATH"; //$NON-NLS-1$
JavaClasspathTab.performApply()
> try {
> IRuntimeClasspathEntry[] boot = fBootpathViewer.getEntries();
> IRuntimeClasspathEntry[] user = fClasspathViewer.getEntries();
> List mementos = new ArrayList(boot.length + user.length);
> for (int i = 0; i < boot.length; i++) {
> boot[i].setClasspathProperty(IRuntimeClasspathEntry.BOOTSTRA P_CLASSES);
> mementos.add(boot[i].getMemento());
> }
> for (int i = 0; i < user.length; i++) {
> user[i].setClasspathProperty(IRuntimeClasspathEntry.USER_CLA SSES);
> mementos.add(user[i].getMemento());
> }
> configuration.setAttribute(IJavaLaunchConfigurationConstants .ATTR_CLASSPATH, mementos);
> } catch (CoreException e) {
> JDIDebugUIPlugin.errorDialog(LauncherMessages.getString("JavaClasspathTab.Unable_to_save_classpath_1 "), e); //$NON-NLS-1$
> }
StandardClasspathProvider.Java
> /**
> * Returns a collection of runtime classpath entries that are defined in the
> * specified attribute of the given launch configuration. When present,
> * the attribute must contain a list of runtime classpath entry mementos.
> *
> * @param configuration launch configuration
> * @param attribute attribute name containing the list of entries
> * @return collection of runtime classpath entries that are defined in the
> * specified attribute of the given launch configuration
> * @exception CoreException if unable to retrieve the list
> */
> protected IRuntimeClasspathEntry[] recoverRuntimePath(ILaunchConfiguration configuration, String attribute) throws CoreException {
> List entries = (List)configuration.getAttribute(attribute, Collections.EMPTY_LIST);
> IRuntimeClasspathEntry[] rtes = new IRuntimeClasspathEntry[entries.size()];
> Iterator iter = entries.iterator();
> int i = 0;
> while (iter.hasNext()) {
> rtes[i] = JavaRuntime.newRuntimeClasspathEntry((String)iter.next());
> i++;
> }
> return rtes;
> }
--
Regards,
Sam Mesh - http://openrules.com
|
|
|
Powered by
FUDForum. Page generated in 0.03260 seconds