Very basic question about executing a class sans jar

R

Ramon F Herrera

I am pretty sure I have solved this problem before, but would like to
hear comments about it.

I know that if I am going to run a simple class (for a console,
tutorial type of program) with no jar I better make it part of the
default package, or else I am looking for trouble.

What is the fix when I try to run a class like this:

java MyProgram // (the file name is 'MyProgram.class')

and I get a message about a wrong name? Let's say that the class was
compiled inside 'mypackage' package.

-Ramon
 
A

Andrew Thompson

Ramon said:
I am pretty sure I have solved this problem before, but would like to
hear comments about it.

I know that if I am going to run a simple class (for a console,
tutorial type of program) with no jar..

In the age of ant, that is just silly.
.. I better make it part of the
default package, or else I am looking for trouble.

Make it a jar with a manifest that specifies main().

The user can either:
a) Double click the damn thing, or..
b) From the command line...
java -jar the.jar

Easy peasy.

You have an unusual (and intriguing) way of turning
simple things into 'complex problems'.
 
O

Owen Jacobson

I am pretty sure I have solved this problem before, but would like to
hear comments about it.

I know that if I am going to run a simple class (for a console,
tutorial type of program) with no jar I better make it part of the
default package, or else I am looking for trouble.

What is the fix when I try to run a class like this:

java MyProgram // (the file name is 'MyProgram.class')

and I get a message about a wrong name? Let's say that the class was
compiled inside 'mypackage' package.

-Ramon

I think it helps to understand what the 'java' command is doing under
the hood. The conception that java "runs class files" is an easy one
to have, and unfortunately is a pretty poor map to reality. What java
(ignoring -jar, for the moment) does is:

1. It constructs a JVM using the bootstrap classpath.
2. It parses $CLASSPATH and the -cp/-classpath argument into a list of
URLs.
3. It tells the JVM to create a URLClassLoader using the URLs from
step 2.
4. It tells the JVM to load the *class* specified on the command line
using the ClassLoader from step 3.
5. It checks for a main method and, if it finds one, calls it.

The practical upshot is that the java command runs classes, instead,
which are located using the normal ClassLoader rules, regardless of
whether they're inside a JAR or not.

In your example you have a class named "mypackage.MyProgram". You
must always run it using the class name 'mypackage.MyProgram' - the
classloaders involved will reject the class if you try to load it
under a different name ('MyProgram' on its own, for example). You
must also ensure that the classpath contains the root of the package
directory structure the classloader expects.

So, if you were in the directory containing MyProgram.class (the class
file), if the directory name ends in '/mypackage/' then you can run
the program using
java -cp .. mypackage.MyProgram
or
java -cp /absolute/path/to/parent/dir myPackage.MyProgram

but not
java MyProgram
or
java MyProgram.class

-o
 
M

Mark Space

Ramon said:
I am pretty sure I have solved this problem before, but would like to
hear comments about it.

I know that if I am going to run a simple class (for a console,
tutorial type of program) with no jar I better make it part of the
default package, or else I am looking for trouble.

I agree with Andrew, this is not true at all.
What is the fix when I try to run a class like this:

1. Make sure you are in the directory above where the .class file is.
2. Type ls mypackage/MyProgram.class just to make sure.
3. java mypackage.MyProgram

4. You can also add the to the classpath with java -cp path
package.MyClass but the path can get long. From the command line, cd to
the directory above where the classes (and packages) are is the easiest.
 
R

Ramon F Herrera

In the age of ant, that is just silly.


Make it a jar with a manifest that specifies main().

The user can either:
a) Double click the damn thing, or..
b) From the command line...
java -jar the.jar

Easy peasy.

You have an unusual (and intriguing) way of turning
simple things into 'complex problems'.

I am just testing the boundaries (*), but if you need a justification,
I can provide it: I happen to have a remote server that came with GNU
Java and it doesn't recognize some jars.

I don't know ant yet. It is in my long queue of things to learn.

-Ramon

(*) those between what works, and what doesn't -and why
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top