简体   繁体   中英

Exception in thread “main” error when trying to run a java program with objectdraw

Primer:

I'm starting a Java class at UCSD next week and our textbook has us download a library called objectdraw.jar which we will be using for the first two chapters. I downloaded the library and placed it in a folder called java_libraries in my ~/home/dev/ directory.

I am trying to run my Java program on Linux and I'm getting an error when I try to run it. It compiles fine, but does not run.

To compile, I'm issuing the following command from the directory where my TouchyWindow.java file exists ~/dev/java/ :

javac -classpath ../java_libraries/objectdraw.jar TouchyWindow.java

NOTE: It compiles without error or warning.

To run the program, I'm issuing the following command from the directory where my TouchWindow.class file exists ~/dev/java/ :

java -classpath ../java_libraries/objectdraw.jar TouchyWindow

When I try to execute the program, I get the following error:

Exception in thread "main" java.lang.NoClassDefFoundError: /home/fhaddad78/dev/java/TouchyWindow
Caused by: java.lang.ClassNotFoundException: .home.fhaddad78.dev.java.TouchyWindow
        at java.net...
        at java.security...
        at java.net...
        at java.lang...
        at sun.misc...
        at java.lang...
Could not find the main class: /home/fhaddad78/dev/java/TouchyWindow. Program will exit.

About my system:

I'm doing this on Gentoo Linux using the Iceatea Java SDK. I mention this because I'm not sure if that could cause the problem.

I'm new to Java and not really sure what this means since the file it can not find is in the path it's saying it can't find it in.

UPDATE To help illustrate the program, I will past the source code as it's only a few lines.

// TouchyWindow.java

import objectdraw.*;
import java.awt.*;

public class TouchyWindow extends WindowController {
  public void onMousePress(Location point) {
    new Text("I'm Touched", 40, 50, canvas);
  }
  public void onMouseRelease(Location point) {
    canvas.clear();
  }
}

The objectdraw.jar library file handles whatever may appear to be missing in my source file.

UPDATE For curiosity sake, I booted into Windows, installed the Java SDK and Eclipse. Created a new project, used the same code as above, and all compiled and worked without any problems. Does that help at all with possible guidance as to the problem?

UPDATE regarding objectdraw.jar

Could this be my problem?

The following was take from the textbook's website.

Using objectdraw with applications rather than applets There is a simple way of using objectdraw with applications that is new with version 1.1 and later of the objectdraw library. Under these version of objectdraw, you can run extensions of WindowController (or Controller) as applications by using a new method named startController. This method is included in the Controller class (and thus is inherited by WindowController).

To turn what would have been an applet into a program that can also be run as an application, simply add the following method to your class that extends Controller or WindowController:

public static void main(String[] args) { new MyClassName().startController(400,400); }

In the above, MyClassName is a placeholder for the name of the class extending Controller, while the parameters (400,400) specify the size of the window desired.

You need to add the directory where the .class file exits to class path.

The output of javac goes to a directory, let us call it BUILD_DIR. Add this build dir to class path

java -classpath ../java_libraries/objectdraw.jar:$BUILD_DIR TouchyWindow

If the TouchWindow has some package you need to add the folder where the package begin. The class name in that case will have to be packagename.packagename.ClassName

The library expects you to use applet. It says add following to run as program

public static void main(String[] args) { 
   new MyClassName().startController(400,400); 
} 
  1. Leave off the .jar file from your classpath (java will accept directories and file all .jar files in there automatically)
  2. Fully qualify your class - without the package, java will assume it's at the top level (ie no package)

Try this:

java -cp ../java_libraries com.mypackage.TouchyWindow

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