1

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.

Jayan
  • 18,003
  • 15
  • 89
  • 143
  • What directory are you compiling from and running from? Is the class declared to be present in some package, i.e. any `package dev.java..` at the top of your class file? – Vikdor Sep 22 '12 at 02:22
  • I think I understand your question. I have a single source file called TouchyWindow.java. At the top of the file are two statements import objectdraw.*; and import java.awt.*; the objectdraw.jar library is supposed to obfuscate most of the work in order to avoid explaining a lot of information that must be known prior to developing GUI based programs. I am compiling and running the program from the directory where the source file and .class file reside. –  Sep 22 '12 at 02:25
  • The class which you want to run must have a main. Could you explain more about the library? Where did you download it from? – Jayan Sep 22 '12 at 02:44
  • The library objectdraw is part of my text book for my CSE11 class at UCSD. The sample programs I have looked through do not contain a main function either. A link to the library can be found at http://eventfuljava.cs.williams.edu/. The library can also be downloaded from there and sample programs are posted as well. –  Sep 22 '12 at 02:48
  • OK, for curiosity sake, I rebooted into Windows, installed Eclipse, and the Java SDK from Oracle, created a new Java project in Eclipse, added the objectdraw.jar library to the project and the same code as above worked without error. –  Sep 22 '12 at 02:56
  • That is no way to solve this problem! Glad it worked. – Jayan Sep 22 '12 at 03:02

2 Answers2

0

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); 
} 
Jayan
  • 18,003
  • 15
  • 89
  • 143
  • When I do that, I get a new error message that says 'Exception in thread "main" java.lang.NoSuchMethodError: main' which I imagine means it can not find the entry point to the program. Similar to main() in C/C++. Supposedly the objectdraw.jar file handles all this. At least from the text I'm reading, it should. –  Sep 22 '12 at 02:32
  • Thank you, I just read that and noticed you updated it in your post as well. My next questions, is what if I wanted to run my program as an applet instead? How would I do that from Linux? BTW, when I rebooted into Linux and added the above code snippet, everything worked. –  Sep 22 '12 at 03:11
  • @ fhaddad78 : Good to see the real fix working. As for making applet working in linux, it should be same as windows. An HTML page with appropriate entries or an appletviewer -- http://docs.oracle.com/javase/6/docs/technotes/tools/windows/appletviewer.html. I am sure it is covered in many SO questions – Jayan Sep 22 '12 at 05:40
0
  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
Bohemian
  • 412,405
  • 93
  • 575
  • 722