Skip to main content



      Home
Home » Language IDEs » Java Development Tools (JDT) » Setting classpath programmatically
Setting classpath programmatically [message #164842] Wed, 16 June 2004 21:35 Go to next message
Eclipse UserFriend
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 #164854 is a reply to message #164842] Wed, 16 June 2004 23:55 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: smesh.openrules.com

Use IClasspathEntry. Hope this helps.

--
Sam Mesh - http://openrules.com
Re: Setting classpath programmatically [message #164975 is a reply to message #164854] Thu, 17 June 2004 13:38 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: dtwchiu.hotmail.com

Sam Mesh wrote:

> Use IClasspathEntry. Hope this helps.

Sam,

I tried to use IClasspathEntry. However, I got ClassCastException in
LaunchConfigurationInfo.createListElement() when it tries to cast the
IClasspathEntry object to a String.

Any idea?

Thanks.

Sam.
Re: Setting classpath programmatically [message #165014 is a reply to message #164975] Thu, 17 June 2004 16:39 Go to previous message
Eclipse UserFriend
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
Previous Topic:plugin access to a policy file
Next Topic:Why "Open on type" menu item in console right-click popup menu disapears in Eclipse 2.1.3
Goto Forum:
  


Current Time: Tue May 13 10:27:54 EDT 2025

Powered by FUDForum. Page generated in 0.03260 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top