Very stupid and trivial question

B

Baba

OK. Here's the thing.

My env is CLASSPATH=C:\javaExercise

My source and classes are in C:\javaExercise\mypackage

Source looks like this (shortened)


package mypackyge;

public class MyClass {

public void printMe(String s) {System.out.println(s)}

public static void main(String args[]) {
MyClass myclass=new MyClass();
myclass.printMe("Hi!");
}
}

I can compile this, but on running it it throws an ClassDefNotFound
exception on the first line of the main method. Why? Is it not possible to
have a main method in a class inside a package?

I'm kinda confused where I err. So, can anyone please help me?
 
G

Guest

OK. Here's the thing.

My env is CLASSPATH=C:\javaExercise

My source and classes are in C:\javaExercise\mypackage

Source looks like this (shortened)


package mypackyge;

public class MyClass {

public void printMe(String s) {System.out.println(s)}

public static void main(String args[]) {
MyClass myclass=new MyClass();
myclass.printMe("Hi!");
}
}
}
I can compile this, but on running it it throws an ClassDefNotFound
exception on the first line of the main method. Why? Is it not possible to
have a main method in a class inside a package?

I'm kinda confused where I err. So, can anyone please help me?

java mypackage.MyClass

Note that java takes the fully qualified name of the class. You might
also want to check that the compiler didn't put the .class file in
C:\javaExercise\mypackage\mypackage .

HTH,
La'ie Techie
 
D

Dario

Baba said:
OK. Here's the thing.

My env is CLASSPATH=C:\javaExercise

My source and classes are in C:\javaExercise\mypackage

Source looks like this (shortened)

package mypackyge;

public class MyClass {

public void printMe(String s) {System.out.println(s)}

public static void main(String args[]) {
MyClass myclass=new MyClass();
myclass.printMe("Hi!");
}
}

I can compile this, but on running it it throws an ClassDefNotFound
exception on the first line of the main method. Why? Is it not possible to
have a main method in a class inside a package?

I'm kinda confused where I err. So, can anyone please help me?

To run it from directory C:\javaExercise write:
java mypackage.MyClass
 
T

Thomas Weidenfeller

Berlin said:
Learn ANT as quickly as possible also, saves a lot of time on these
problems.

And creates others. The key to such problems ist not to throw yet
another technology at it. The key is to learn and understand the
mechanisms first. Once you have understood the principles you can debug
such problems independent of the build tool in use. And you can make an
informed decission about a build tool.

/Thomas
 
B

Berlin Brown

LÄÊ»ie Techie said:
OK. Here's the thing.

My env is CLASSPATH=C:\javaExercise

My source and classes are in C:\javaExercise\mypackage

Source looks like this (shortened)


package mypackyge;

public class MyClass {

public void printMe(String s) {System.out.println(s)}

public static void main(String args[]) {
MyClass myclass=new MyClass();
myclass.printMe("Hi!");
}
}
}
I can compile this, but on running it it throws an ClassDefNotFound
exception on the first line of the main method. Why? Is it not possible to
have a main method in a class inside a package?

I'm kinda confused where I err. So, can anyone please help me?


java mypackage.MyClass

Note that java takes the fully qualified name of the class. You might
also want to check that the compiler didn't put the .class file in
C:\javaExercise\mypackage\mypackage .

HTH,
La'ie Techie
Learn ANT as quickly as possible also, saves a lot of time on these
problems.
 
J

John C. Bollinger

Baba said:
OK. Here's the thing.

My env is CLASSPATH=C:\javaExercise

My source and classes are in C:\javaExercise\mypackage

Source looks like this (shortened)


package mypackyge;

public class MyClass {

public void printMe(String s) {System.out.println(s)}

public static void main(String args[]) {
MyClass myclass=new MyClass();
myclass.printMe("Hi!");
}
}

I can compile this, but on running it it throws an ClassDefNotFound
exception on the first line of the main method. Why? Is it not possible to
have a main method in a class inside a package?

I'm kinda confused where I err. So, can anyone please help me?

If that code is pasted from your actual source then you appear to have
misspelled your package name in the package statement. Consult also the
other responses.


John Bollinger
(e-mail address removed)
 
B

Baba

Thanx for the suggestions. The source ain't copy pasted so the typo wasn't a
problem.

The problem was that I tried to run the "main" method from the package
directory, not from the one above. Now it works perfectly.

I'd like to add few things.

1) I am not a novice in Java, actually I'm programming with it for a few
years. The fact I DID have a problem with this thing is that I always used
some kind of software shortcuts (an IDE interface, ant...) and never (or
rarely) programmed with skeleton tools (textpad, javac and java). I came up
with the problem when a friend, a Java beginner asked me about it and it
turned out I didn't know the answer myself.
To put it bluntly, don't use advanced tools before you learn the
ground-level basics.

2) I didn't find anywhere in Java documentation that the "main" method from
packaged classes couldn't be executed from the package dir, or could I find
any logic in that. How come that is so and why isn't it documented better, I
wonder?

Thnx for all your help, btw.
 
T

Tony Morris

1) I am not a novice in Java, actually I'm programming with it for a few
I am not of the opinion that number of years has any implication on ability
to use.
Rather, the ability to learn over those years (or whatever time period).
The problem was that I tried to run the "main" method from the package
directory, not from the one above.
This is a typical mistake of someone who is inexperienced in "problem
resolution" (a distinct ability from programming)
2) I didn't find anywhere in Java documentation that the "main" method from
packaged classes couldn't be executed from the package dir, or could I find
any logic in that. How come that is so and why isn't it documented better, I
wonder?
You need to read the packages tutorial on the http://java.sun.com web site.
Another very useful tool in programming is the ability to locate resources
(usually documentation).

This is not a personal attack, hopefully an eye-opener; at least, that is my
intention.
http://www.apa.org/journals/psp/psp7761121.html

--
Tony Morris
(BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
Software Engineer
IBM Australia - Tivoli Security Software
(2003 VTR1000F)
 
P

P.Hill

Baba said:
2) I didn't find anywhere in Java documentation that the "main" method from
packaged classes couldn't be executed from the package dir, or could I find
any logic in that. How come that is so and why isn't it documented better, I
wonder?

You can run anything you want from anywhere you want as long as the directory
containing the root name of the package name is ON YOUR CLASSPATH VARIABLE.
so if classpath included CLASSPATH=C:\javaExercise you then need
to say
java myPackage.MyClass

NO MATTER WHAT DIRECTORY YOU ARE IN WHEN YOU TYPE THE COMMAND

-Paul
 
J

John C. Bollinger

Baba said:
Thanx for the suggestions. The source ain't copy pasted so the typo wasn't a
problem.

The problem was that I tried to run the "main" method from the package
directory, not from the one above. Now it works perfectly.

I'd like to add few things.

1) I am not a novice in Java, actually I'm programming with it for a few
years. The fact I DID have a problem with this thing is that I always used
some kind of software shortcuts (an IDE interface, ant...) and never (or
rarely) programmed with skeleton tools (textpad, javac and java). I came up
with the problem when a friend, a Java beginner asked me about it and it
turned out I didn't know the answer myself.
To put it bluntly, don't use advanced tools before you learn the
ground-level basics.

Definately a sound piece of advice that you'll also hear others around
here offer from time to time.
2) I didn't find anywhere in Java documentation that the "main" method from
packaged classes couldn't be executed from the package dir, or could I find
any logic in that. How come that is so and why isn't it documented better, I
wonder?

It is not documented because it isn't so. You are dealing with a
classpath problem. If you provide a suitable classpath you can run the
main() method of any class from any working directory. In your
particular case, the classpath you wanted was probably "..". Also
contributing to your confusion is that if no classpath is explicitly
specified the java launcher includes the current working directory in
the effective classpath, which diguises the fact that the classpath is
relevant.


John Bollinger
(e-mail address removed)
 
N

NOBODY

Very stupid and trivial question

You meant: "Very stupid and useless news message subject"

You will not get answer with title like that.
Beside, you are not helping people that are searching for the same answer
as you.
 
N

nos

Tony Morris said:
1) I am not a novice in Java, actually I'm programming with it for a few
years.
I am not of the opinion that number of years has any implication on ability
to use.
Rather, the ability to learn over those years (or whatever time period).
The problem was that I tried to run the "main" method from the package
directory, not from the one above.
This is a typical mistake of someone who is inexperienced in "problem
resolution" (a distinct ability from programming)
2) I didn't find anywhere in Java documentation that the "main" method from
packaged classes couldn't be executed from the package dir, or could I find
any logic in that. How come that is so and why isn't it documented
better,
I
wonder?
You need to read the packages tutorial on the http://java.sun.com web site.
Another very useful tool in programming is the ability to locate resources
(usually documentation).

This is not a personal attack, hopefully an eye-opener; at least, that is my
intention.
http://www.apa.org/journals/psp/psp7761121.html

--
Tony Morris
(BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
Software Engineer
IBM Australia - Tivoli Security Software
(2003 VTR1000F)
I have win xp pro. I downloaded java 1.4.2 from sun
and installed with a simple click. Now, with no effort,
I can run any java program located anywhere even
if it is not in the current directory. I just have to set
the classpath. Maybe you should start over.
 
T

Tony Morris

nos said:
Tony Morris said:
I am not of the opinion that number of years has any implication on ability
to use.
Rather, the ability to learn over those years (or whatever time period).

This is a typical mistake of someone who is inexperienced in "problem
resolution" (a distinct ability from programming)
better,
You need to read the packages tutorial on the http://java.sun.com web site.
Another very useful tool in programming is the ability to locate resources
(usually documentation).

This is not a personal attack, hopefully an eye-opener; at least, that
is
my
intention.
http://www.apa.org/journals/psp/psp7761121.html

--
Tony Morris
(BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
Software Engineer
IBM Australia - Tivoli Security Software
(2003 VTR1000F)
I have win xp pro. I downloaded java 1.4.2 from sun
and installed with a simple click. Now, with no effort,
I can run any java program located anywhere even
if it is not in the current directory. I just have to set
the classpath. Maybe you should start over.

What does that have to do with anything ?

--
Tony Morris
(BInfTech, Cert 3 I.T.)
Software Engineer
IBM Australia - Tivoli Security Software
(2003 VTR1000F)
Sun Certified Programmer for the Java 2 Platform (1.4)
Sun Certified Developer for the Java 2 Platform
 
N

nos

Tony Morris said:
nos said:
Tony Morris said:
1) I am not a novice in Java, actually I'm programming with it for a few
years.
I am not of the opinion that number of years has any implication on ability
to use.
Rather, the ability to learn over those years (or whatever time period).

The problem was that I tried to run the "main" method from the package
directory, not from the one above.
This is a typical mistake of someone who is inexperienced in "problem
resolution" (a distinct ability from programming)

2) I didn't find anywhere in Java documentation that the "main" method
from
packaged classes couldn't be executed from the package dir, or could I
find
any logic in that. How come that is so and why isn't it documented better,
I
wonder?
You need to read the packages tutorial on the http://java.sun.com web site.
Another very useful tool in programming is the ability to locate resources
(usually documentation).

This is not a personal attack, hopefully an eye-opener; at least, that
is
my
intention.
http://www.apa.org/journals/psp/psp7761121.html

--
Tony Morris
(BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
Software Engineer
IBM Australia - Tivoli Security Software
(2003 VTR1000F)
I have win xp pro. I downloaded java 1.4.2 from sun
and installed with a simple click. Now, with no effort,
I can run any java program located anywhere even
if it is not in the current directory. I just have to set
the classpath. Maybe you should start over.

What does that have to do with anything ?

The problem was that I tried to run the "main" method from the package
directory, not from the one above.

So you tried it and it worked?
 
T

Tony Morris

nos said:
Tony Morris said:
could
I
find
any logic in that. How come that is so and why isn't it documented
better,
I
wonder?
You need to read the packages tutorial on the http://java.sun.com web
site.
Another very useful tool in programming is the ability to locate resources
(usually documentation).

This is not a personal attack, hopefully an eye-opener; at least,
that
is
my
intention.
http://www.apa.org/journals/psp/psp7761121.html

--
Tony Morris
(BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
Software Engineer
IBM Australia - Tivoli Security Software
(2003 VTR1000F)


I have win xp pro. I downloaded java 1.4.2 from sun
and installed with a simple click. Now, with no effort,
I can run any java program located anywhere even
if it is not in the current directory. I just have to set
the classpath. Maybe you should start over.

What does that have to do with anything ?

The problem was that I tried to run the "main" method from the package
directory, not from the one above.

So you tried it and it worked?
--
Tony Morris
(BInfTech, Cert 3 I.T.)
Software Engineer
IBM Australia - Tivoli Security Software
(2003 VTR1000F)
Sun Certified Programmer for the Java 2 Platform (1.4)
Sun Certified Developer for the Java 2 Platform

I assume that was caused by a lack of understanding of how packages are used
in Java.
http://java.sun.com/docs/books/tutorial/java/interpack/packages.html


--
Tony Morris
(BInfTech, Cert 3 I.T.)
Software Engineer
IBM Australia - Tivoli Security Software
(2003 VTR1000F)
Sun Certified Programmer for the Java 2 Platform (1.4)
Sun Certified Developer for the Java 2 Platform
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top