gpt4 book ai didi

java - 项目未在 Eclipse 中创建

转载 作者:行者123 更新时间:2023-12-02 13:16:20 24 4
gpt4 key购买 nike

我正在尝试使用处理程序类将我的插件项目作为应用程序运行:

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
IWorkbenchWindow window =
HandlerUtil.getActiveWorkbenchWindowChecked(event);
MessageDialog.openInformation(
window.getShell(), "MenuEclipseArticle Plug-in",
"Hello, Eclipse world");
CreateProject create=new CreateProject();
IPath projectLocation = new Path("C:/Users/monika.sharma/Desktop/software");
String projectName = "myProject1234";
create.createProject(projectLocation, projectName);

return null;
}

我创建了一个 createProject Java 类来创建一个包含 srcbin 文件夹的项目。

public class CreateProject {

static boolean createAtExternalLocation = false;
static IPath projectLocation=null;
private IProject project;
private IJavaProject javaProject;

public void createProject(IPath projectLocation,String projectName)
{
IProgressMonitor progressMonitor = new NullProgressMonitor();
IProject project =ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
IProjectDescription description =ResourcesPlugin.getWorkspace().newProjectDescription(project .getName());
description.setLocation(projectLocation);

try {
project.create(description, progressMonitor);
} catch (CoreException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
project.open(progressMonitor);
} catch (CoreException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
setJavaNature();
} catch (CoreException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Add JRE
try {
addDefaultJRE(progressMonitor);
} catch (JavaModelException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Create the bin folder
try {
createBinFolder();
} catch (CoreException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Create a source folder
try {
IPackageFragmentRoot src = createSourceFolder("src");
} catch (CoreException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void setJavaNature() throws CoreException {
// Get the description of the project
IProjectDescription description = project.getDescription();
// Change the description with NATURE_ID of JavaCore
description.setNatureIds(new String[] {JavaCore.NATURE_ID});
// Set the description back to the project
project.setDescription(
description, null);
}
public void addDefaultJRE(IProgressMonitor progressMonitor) throws JavaModelException {
// Create an empty class path entry array for the project
javaProject.setRawClasspath(
new IClasspathEntry[0], progressMonitor);
// Get it - the old entries
IClasspathEntry[] oldEntries = javaProject.getRawClasspath();
// Increase 1 for the size of the new entry array.
IClasspathEntry[] newEntries = new IClasspathEntry[oldEntries.length + 1];
// Copy the old entries to new entry array.
System.arraycopy(
oldEntries, 0, newEntries, 0, oldEntries.length);
// Set the new element to the default JRE of the system.
newEntries[oldEntries.length] = JavaRuntime.getDefaultJREContainerEntry();
// Set back entry paths to the project
javaProject.setRawClasspath(
newEntries, progressMonitor);
}
public IFolder createBinFolder() throws CoreException {
// Get the folder with the name bin
IFolder binFolder = project.getFolder("bin");
// Create this folder. if the first parameter is false, it does not
// forced to override the folder
binFolder.create(
false, true, null);
// Get the path of the bin folder
IPath outputLocation = binFolder.getFullPath();
// Set that path as the output location of the project
javaProject.setOutputLocation(
outputLocation, null);
return binFolder;
}
public IPackageFragmentRoot createSourceFolder(String srcName) throws CoreException {
// Get the folder with srcName name. It may be not exist
IFolder folder = project.getFolder(srcName);
// Create a real folder. In the case you want to force override the
// folder,
// set the first parameter as true
folder.create(
false, true, null);
// Get the source folder as root package
IPackageFragmentRoot root = javaProject.getPackageFragmentRoot(folder);
// Do the samething on the addDefaultJRE method
IClasspathEntry[] oldEntries = javaProject.getRawClasspath();
IClasspathEntry[] newEntries = new IClasspathEntry[oldEntries.length + 1];
System.arraycopy(
oldEntries, 0, newEntries, 0, oldEntries.length);
// Add a new source folder as a new entry for class paths
newEntries[oldEntries.length] = JavaCore.newSourceEntry(root.getPath());
// Set back entry paths to the project
javaProject.setRawClasspath(
newEntries, null);
return root;
}

当我使用“作为 Eclipse 应用程序运行”运行 MANIFEST.MF 文件时,仅打开 Eclipse,但未创建项目。

最佳答案

“作为 Eclipse 应用程序运行”只是启动一个包含您的插件的新 Eclipse。新的 Eclipse 使用单独的工作区。有关详细信息,请查看“运行 > 运行配置”对话框的“Eclipse 应用程序”部分。

这不会调用您的处理程序代码。仅当从菜单项或工具栏按钮等调用时,处理程序才会运行。必须使用 org.eclipse.ui.handlers 扩展点声明处理程序。有关处理程序的更多详细信息,请阅读类似 this 的内容。 .

关于java - 项目未在 Eclipse 中创建,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43754545/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com