Java类路径问题

发布于 2024-11-02 18:54:07 字数 1838 浏览 1 评论 0原文

运行以下代码时出现错误:

import java.awt.*;
import javax.swing.*;

import org.fife.ui.rtextarea.*;
import org.fife.ui.rsyntaxtextarea.*;

public class TextEditorDemo extends JFrame {

   private static final long serialVersionUID = 1L;


   public TextEditorDemo() {

      JPanel cp = new JPanel(new BorderLayout());

      RSyntaxTextArea textArea = new RSyntaxTextArea();
      textArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_JAVA);
      RTextScrollPane sp = new RTextScrollPane(textArea);
      cp.add(sp);

      setContentPane(cp);
      setTitle("RSyntaxTextArea 1.4 - Example 1 - Text Editor Demo");
      setDefaultCloseOperation(EXIT_ON_CLOSE);
      pack();
      setLocationRelativeTo(null);

   }

   public static void main(String[] args) {
      // Start all Swing applications on the EDT.
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            new TextEditorDemo().setVisible(true);
         }
      });
   }
}

当我使用 java -classpath rsyntaxtextarea.jar; 运行时。 TextEditorDemo,我没有得到输出。 我收到的错误是:

Exception in thread "main" java.lang.NoClassDefFoundError: TextEditorDemo
Caused by: java.lang.ClassNotFoundException: TextEditorDemo
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: TextEditorDemo.  Program will exit.

任何人都可以帮忙吗!此示例取自 RSyntaxTextArea

I have error while running this below code:

import java.awt.*;
import javax.swing.*;

import org.fife.ui.rtextarea.*;
import org.fife.ui.rsyntaxtextarea.*;

public class TextEditorDemo extends JFrame {

   private static final long serialVersionUID = 1L;


   public TextEditorDemo() {

      JPanel cp = new JPanel(new BorderLayout());

      RSyntaxTextArea textArea = new RSyntaxTextArea();
      textArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_JAVA);
      RTextScrollPane sp = new RTextScrollPane(textArea);
      cp.add(sp);

      setContentPane(cp);
      setTitle("RSyntaxTextArea 1.4 - Example 1 - Text Editor Demo");
      setDefaultCloseOperation(EXIT_ON_CLOSE);
      pack();
      setLocationRelativeTo(null);

   }

   public static void main(String[] args) {
      // Start all Swing applications on the EDT.
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            new TextEditorDemo().setVisible(true);
         }
      });
   }
}

When i run using java -classpath rsyntaxtextarea.jar;. TextEditorDemo, i'm not getting the output.
i'm getting error instead as :

Exception in thread "main" java.lang.NoClassDefFoundError: TextEditorDemo
Caused by: java.lang.ClassNotFoundException: TextEditorDemo
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: TextEditorDemo.  Program will exit.

Can anyone help! This example was taken from, RSyntaxTextArea

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怪我入戏太深 2024-11-09 18:54:07

这是类路径的问题。

java -classpath rsyntaxtextarea.jar;. TextEditorDemo

按照目前的设置,虚拟机将期望在运行“java”的同一目录中找到“TextEditorDemo.class”,并且“rsyntaxarea.jar”也在该目录中。检查这些文件是否确实位于当前目录中。如果没有,请将所需的路径信息添加到 jar 中,以及 TextEditorDemo.class 文件的位置。

编辑:原始类路径有“;”。最后 - 问题已被编辑并被删除。
这 ;。是必需的,以便从当前目录加载类。

This is a problem with the classpath.

java -classpath rsyntaxtextarea.jar;. TextEditorDemo

As presently set up, the vm will expect to find "TextEditorDemo.class" in the same directory as you are running "java" from, and that "rsyntaxarea.jar" is also in that directory. Check that these files are indeed in the current directory. If not, add the needed path information to the jar, and the location of the TextEditorDemo.class file.

EDIT: The original classpath has ";." at the end - the question has been edited and this removed.
The ;. is necessary so that classes are loaded from the current directory.

节枝 2024-11-09 18:54:07

你的类路径是错误的。它只会在 jar 文件中查找类。要了解如何将类路径设置为 jar 文件和类,请查看我在这个问题中的答案: 编译sample.java和jgraph_5.8.3.1.jar

Your classpath is wrong. It will only find class in the jar file. To know how to set classpath to a jar file and to your class look at my answer in this question: compile sample.java and jgraph_5.8.3.1.jar

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文