How to load classes in an eclipse plug-in - class

I want to load a class such as database driver from the user's workspace within my eclipse plug-in. Is there an easy way to do this?

I believe that you can add a project as a resource to another in Eclipse, then you just import through the package hierarchy as usual.

Related

intellij idea module default "user.dir"

I am fresh about intellij idea. I just import several projects from eclipse, and it's modules in intellij idea.
I run one main in one module and get file can not found exception. I check the user.dir parameter and find it direct to another module(that module is the first module when I import eclipse projects).
System.out.println(System.getProperty("user.dir"));
I just want set the user.dir to that module directory when I run the main in one module. How to set it?
Create a Run/Debug configuration for your Main class (the easiest way is to right-click on it and choose Save 'MyClass.main()').
Now, find your configuration (named after your class) on the main toolbar dropdown and choose Edit Configurations
For VM options, put -Duser.dir=whatever
Alternatively, setting Working directory might be able to help you.

Importing a existing Web Application , into Eclipse

I need to import a existing Web Application , into Eclipse .
Please see the Structure of my Web Application as shown in the below figure .
http://imageshack.us/f/220/structurek.jpg/
From Eclipse IDE , while using import What option i need to select that is should i use
Existing projects into Eclipse
Archive File
File System
please see this image
http://imageshack.us/f/850/eclipseo.jpg/
Import existing projects into Eclipse works only for projects that were created in Eclipse.
And you're definitely not dealing with an Archive File here.
Import from the File System just copies the resources but does not actually create an Eclipse project for you.
What I would advise you, is to create a new Dynamic Web Project, configuring all the required facets, and then just copy all the contents of your existing app to the WebContent folder, either by drag'n'dropping it into the Project Explorer or by using Import from the File System, overwriting all the contents.
So far, there seems to be no other way to do it in Eclipse. However you may check out the similar post. The user #RC recommends using ant task for this process, but I'm more than sure that it won't configure all the required facets for you. It may work for some simple Java projects, but surely not enough for Java EE projects.

How to import user libraries programmatically from a file

I have "myUserLib.userlibraries" file with all the required user libraries defined. As part of my plug-in that i developed (this will create web projects and required settings), i want to import this file to the projects created by the plug-in.
Is there any programatic way to import libraries from a file?
If you are developing a plugin to add these libraries to project classpath, I recommend that you consider writing a custom classpath container rather than re-using the user libraries facility. If you write a custom container, project classpath would include id of your container and JDT will query your code at runtime to get the actual list of classpath entries.
This will save you from having to store and maintain the contents of your library in workspace preferences, as you would have to do if you took the user library approach.
See org.eclipse.jdt.core.IClasspathContainer class.
Finally i got this worked the way i want. I am now able to programmatically upload the userlibraries into workspace preferences from "myuserlibs.userlibraries" file. I used following eclipse plugins classes.
org.eclipse.jdt.internal.core.UserLibrary.class
org.eclipse.jdt.internal.core.UserLibraryManager.class
org.eclipse.jdt.internal.core.UserLibraryClasspathContainer.class
org.eclipse.jdt.internal.core.UserLibraryClasspathContainerInitializer.class
These classes have bunch of methods to achieve this task.
Once they got uploaded in to preferences, i am also able to programmatically add required user library entries into a respective projects (created by my plugin). Because i have around 8 web projects in my workspace and all of them required different set of user libraries in their classpath container.

How the Eclipse implements importing project?

How the Eclipse implements importing project and which part of the Eclipse's codes works?
OK, we all know EC has function:file->import ->exsit project ...I am looking into the code of EC, but i cant find which part of it is used to implement this loading work.
In the part of Eclipse that you want learn more about (in this case for example in the Import Projects Wizard), you can open the Plug-in Spy using Alt+Shift+F1. It will tell you the class and the plug-in contributing the functionality. You can then check out the plug-in from the Eclipse CVS.

How can I import eclipse JDT classes in a project

I want to do the following imports in a class.
import org.eclipse.jdt.core.dom.*;
import org.eclipse.jdt.core.compiler.CharOperation;
import org.eclipse.jdt.core.compiler.IProblem;
import org.eclipse.jdt.internal.compiler.ClassFile;
import org.eclipse.jdt.internal.compiler.CompilationResult;
import org.eclipse.jdt.internal.compiler.Compiler;
import org.eclipse.jdt.internal.compiler.DefaultErrorHandlingPolicies;
import org.eclipse.jdt.internal.compiler.ICompilerRequestor;
import org.eclipse.jdt.internal.compiler.classfmt.ClassFileReader;
import org.eclipse.jdt.internal.compiler.classfmt.ClassFormatException;
import org.eclipse.jdt.internal.compiler.env.ICompilationUnit;
import org.eclipse.jdt.internal.compiler.env.INameEnvironment;
import org.eclipse.jdt.internal.compiler.env.NameEnvironmentAnswer;
import org.eclipse.jdt.internal.compiler.impl.CompilerOptions;
import org.eclipse.jdt.internal.compiler.problem.DefaultProblemFactory;
import org.eclipse.jface.text.Document;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.text.edits.TextEdit;
How can I import the JDT within Eclipse?
Cheers.
I think I found an easier way to do this:
right-click on your project in the Package Explorer;
choose "Build Path...";
choose "Configure Build Path";
choose the Libraries tab;
click the "Add Variable..." button;
in the list box, choose the "ECLIPSE_HOME" entry, and then click the "Extend" button;
in the list box, open up the "plugins" folder entry, scroll way down, and shift-click all the org.eclipse.jdt.* JAR files that are in the file listing beneath the folders;
click OK until you're all the way back out.
That should do it.
Unless I'm misunderstanding you, you just need to include the JDT JAR files on your classpath; they're all available in your Eclipse plugins directory. So for your project, right-click on the project's name in the Package Explorer, go to the Build Path... submenu, and choose Configure Build Path. Then in the Libraries tab, use the "Add External JARs" button to add each of the relevant JAR files from the Eclipse plugins directory.
If your'e writing plugins for Eclipse, you shouldn't really be trying to instantiate the internal packages. According to this API Rules of Engagement
Stick to officially documented APIs. Only reference packages that are documented in the published API Javadoc for the component. Never reference a package belonging to another component that has "internal" in its name---these are never API. Never reference a package for which there is no published API Javadoc---these are not API either.
For the others, add the package name to the Import-Package entry in your manifest.
There are extension points into the JDT, but if what you want to do falls outside of these, then I'm afraid you're out of luck.
If you're just looking to use a compiler in your code, without relying on the JDK (i.e. on a JRE), then I would consider shipping with a more standalone Java based Java compiler like Janino.
If you need these classes, you are probably in a plug-in project already. You should be able to import these classes by applying the quick fix "Fix project setup..." (Ctrl+1) on the line where Eclipse is complaining about the imports. That will add the required plug-ins to your MANIFEST.MF file in the META-INF directory (org.eclipse.jdt.core and org.eclipse.jface.text in your case). You can also add them manually in your MANIFEST.MF file. If your project is no plug-in project (and you have no MANIFEST.MF file) you can convert it by right-click on the project -> PDE Tools -> Convert Projects to Plug-in Project first. If you add dependencies to plug-in projects in the normal way ("configure build path") the classloading won't work properly at runtime (though it will compile).