On Mon, Jan 03, 2000 at 08:24:42PM -0800, [EMAIL PROTECTED] wrote:
> This might sound ignorant, but everything I've read is about
> java programming syntax and stuff like that. My question is
> how do you run a java program? Just using java ./Clock2 doesn't
> work. I either get "class not found" or "In class Clock2: void
> main(String argv[]) is not defined"  

Java program are always defined in one package. Java uses the filesystem to
store tha package hierarchy creating a subdirectory for each level in the
package name. This means that a package named com.mycompany.mypackage will
generate: ./com/mycompany/mypackage on a Unix system. You compiled .class
files will be stored in the last subdirectory.

At the beginning of your program you should have a declaration like this:

package com.mycompany.mypackage;

After that there should be the code that defines a class. The class must be
public to be runnable and must have a method main() with the following
prototype:

public static void main( String[] args )
{
  //The code here is usually in the form:
  MyClass aClassObj = new MyClass();
  aClassObj.method();
}

If you have an explicit package declaration you should be able to run the
class with: 
  java com.mycompany.mypackage.MyClass
or 
  java -cp lib1:lib2:libn:. com.mycompany.mypackage.MyClass

Where lib1..libn are either names of directories or jarfiles containing
additional packages.

This assumes that the com subdirectory corresponding to the first level of
the package name has been created under the current dir.

If you don't have an explicit package declaration then the class is defined
in the default package and you should be able to run it with something like:

  java -cp . MyClass
  
Using "java ./MyClass" is not going to work because the JVM replaces the "."
in a classname with the directory separator of your OS and tries to locate
the .class in the subdirectory obtained from that sustitution. Of course the
addition of the "/" is making things event messier :). If you're using JDK
1.1 the CLASSPATH is handled in a more complicated way than 1.2 so you
should add the current directory to the CLASSPATH to run classes in the
default package: 

  java -cp .:$CLASSPATH MyClass


Hope this helps,

--Paolo


----------------------------------------------------------------------
To UNSUBSCRIBE, email to [EMAIL PROTECTED]
with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]

Reply via email to