简体   繁体   中英

Java classpath problem

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 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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM