Java 在eclipse中运行时找不到类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2306133/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 05:55:27  来源:igfitidea点击:

Getting class not found Exception when running in eclipse

javaeclipse

提问by GuruKulki

I am running the following code in eclipse but getting a class not found exception:

我在 Eclipse 中运行以下代码,但发现类未找到异常:

         import org.eclipse.jface.window.Window;
         import org.eclipse.swt.SWT;
         import org.eclipse.swt.widgets.Display;
         import org.eclipse.swt.widgets.Shell;



        public class DialogClass {

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub

    System.out.println("TEst");
    Shell frame = new Shell(SWT.SHELL_TRIM);

    PublishGenericArtefactDialog publishGenericArtefactDialog =            
                       new PublishGenericArtefactDialog(frame);

    publishGenericArtefactDialog.setTitle("Test");

    if (publishGenericArtefactDialog.open() == Window.CANCEL){
        try {
            throw new Exception("Cancelled");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
      }
       }

       }

and error i am getting is

我得到的错误是

    TEst
    Exception in thread "main" java.lang.NoClassDefFoundError: org/eclipse/core/runtime/IStatus
at DialogClass.main(DialogClass.java:19)
     Caused by: java.lang.ClassNotFoundException: org.eclipse.core.runtime.IStatus
at java.net.URLClassLoader.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
... 1 more

need help

需要帮忙

采纳答案by VonC

As mentioned in this thread

正如在这个线程中提到的

Have you listed the org.eclipse.core.runtimeas a plugin dependency in the Manfest.MF?
I think that the IStatusis actually in an Equinox package, but the runtime includes the equinox plugin at runtime.
If you're just running it as a Java application (e.g. by sticking Jars on the classpath) then you'll probably need the org.eclipse.equinox.core/runtimeor similar.

Thanks for your suggestion. The problem was solved by adding org.eclipse.equinox.commonand org.eclipse.core.commandsto the Java Build Path property for the project - which I run as an SWT application.

您是否将 列为org.eclipse.core.runtime插件依赖项Manfest.MF
我认为它IStatus实际上在 Equinox 包中,但运行时在运行时包含了 equinox 插件。
如果您只是将它作为 Java 应用程序运行(例如通过在类路径上粘贴 Jars),那么您可能需要org.eclipse.equinox.core/runtime或类似的。

谢谢你的建议。通过向项目的 Java Build Path 属性添加org.eclipse.equinox.common和解决了该问题org.eclipse.core.commands- 我将其作为 SWT 应用程序运行。

As mentioned by AlBluein the comment, the Eclipse wiki on JFaceconfirms:

正如AlBlue在评论中提到的,JFace 上Eclipse wiki确认:

JFace can be used in standalone SWT+JFace apps, without requiring the Eclipse Runtime or other parts of the Eclipse Platform.
This was made easier to do in 3.2 (2006), with the only prerequisites for JFace being reduced to:

  • SWT,
  • the new org.eclipse.equinox.commonplug-in,
  • and org.eclipse.core.commandsplug-in.

For more details, see Bug 49497.

In 3.3 an optional dependency on the org.osgi.frameworkpackage was added which is defined in the org.eclipse.osgi.
If this plug-in is absent JFace will continue to function but without the benefit of internationalization support for its images.

JFace 可以在独立的 SWT+JFace 应用程序中使用,不需要 Eclipse 运行时或 Eclipse 平台的其他部分。
这在 3.2 (2006) 中变得更容易了,JFace 的唯一先决条件被简化为:

  • SWT
  • org.eclipse.equinox.common插件,
  • org.eclipse.core.commands插件。

有关更多详细信息,请参阅错误 49497

在 3.3org.osgi.framework中添加了对包的可选依赖项,该依赖项在org.eclipse.osgi.
如果没有此插件,JFace 将继续运行,但不会为其图像提供国际化支持。

回答by Pentium10

Did you imported something for this?

你为此进口了一些东西吗?

PublishGenericArtefactDialog publishGenericArtefactDialog =            
                       new PublishGenericArtefactDialog(frame);

回答by AlBlue

The classpath for compiling isn't necesarily the same as the runtime classpath. There is a launch config (Run->Run...) will shoe you what is there.

用于编译的类路径不一定与运行时类路径相同。有一个启动配置(Run->Run...)会告诉你那里有什么。

Note that the SWT jar is just the API classes - you'll need a per-os binary for the real runtime classes, so that might be missing. If you add the "swt" classpath container then it should do the right thing.

请注意,SWT jar 只是 API 类 - 您需要一个用于实际运行时类的 per-os 二进制文件,因此可能会丢失。如果您添加“swt”类路径容器,那么它应该做正确的事情。

What's the contents of .classpath inbyout current project?

.classpath inbyout 当前项目的内容是什么?

回答by ally

When you run your exported product, if you see something like,

当您运行导出的产品时,如果您看到类似的内容,

org.osgi.framework.BundleException: 
The activator org.eclipse.ui.internal.WorkbenchPlugin 
for bundle org.eclipse.ui.workbench is invalid

...

Caused by: java.lang.ClassNotFoundException: org.eclipse.swt.SWTError

It may be fixed via:

它可以通过以下方式修复:

  1. Open the product configuration editor on your product file
  2. Click the "Launching" tab
  3. clear out the "Program Arguments" and "VM Arguments" fields.
  4. Re-export the product
  1. 在您的产品文件上打开产品配置编辑器
  2. 单击“启动”选项卡
  3. 清除“程序参数”和“VM 参数”字段。
  4. 再出口产品