Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Dumb question?

  Asked By: Fern    Date: May 05    Category: Java    Views: 473
  

I am in the process of learning java and have been wondering about
this for a little while. I am using JDK 1.3 on a win98se machine.

after I compline to bytecode I get my class file say, Prog1.class
If I run it as:

java Prog1

It runs fine, but if I run it as:

java Prog1.class

I get a NoClassDefFoundError Just curious as to why, if anyone knows.

Share: 

 

10 Answers Found

 
Answer #1    Answered By: Leroy Schmidt     Answered On: May 05

here how you compile the java  program

>javac Prog1.java
>java Prog1

If you get NoClassDefFoundError error then try this

on the command console
>set classpath=;%classpath%
>java Prog1

 
Answer #2    Answered By: Edwin Chavez     Answered On: May 05

incude "." in the classpath for default package.

 
Answer #3    Answered By: Burk Martin     Answered On: May 05

Anyoneuse jBuilder 9 over here ?

 
Answer #4    Answered By: Hubert Taylor     Answered On: May 05

I would guess that your classpath isn't set up right. If you add "." to
your classpath environment variable then it will search what ever
directory you are in when you try and run  a file.

 
Answer #5    Answered By: Lurlene Fischer     Answered On: May 05

When you run  "java Prog1", java  search for a class  named "Prog1" in its class
path (usually current directory), not for a file  named "Prog1" or "Prog1.class".

But when you run "java Prog1.class", java search for a class named "class" in
"Prog1" package in its class path. There's no such class that why  it returns
NoClassDefFoundError.

 
Answer #6    Answered By: Helene Stewart     Answered On: May 05

i am not agree with the theory of classpath with this problem.

venus_night_2000:-
for this we need to understand the structure of java's JRE & JVM. There r
main following components in JRE's JVM
1.class loader
2.verifier
3.compiler
4.interpreter
5.executer
6.security manager

Actually what happen is that, when u say
c:\javac prog1.java
compliler in the JVM compiles your program & detects for error. If no error
found then nothing appeares in command line, that means u'r file  is
error-less
Now, when u says
c:\java prog1
sun's jre is written such a way that is interprets only class  name. so
here interpreter in jvm takes only file name. now, this sun's jre interprets
'prog1' as command line argument with 'java' in command line. Then class
loader searches for the respective class, namely 'prog1.class'. & JRE, which
accepts only file name, executes class file.
U can write your own jre which takes command line argument as u like.
c:\java prog1.class
I mean, if your requirement is to run  file with .class extension, then u
have to modify JRE's JVM & create user-specific JVM's. (BUT THATS TOO
CLUMPZY.)
for ex. some vendor's have created JRE's which executes Garbage Collection
every time to save memory & time of execution. etc.....
Every GUI editor u see, must modifies JRS according to his need, to run prog
faster & with less errors.
I hope this clerify your question.

 
Answer #7    Answered By: Feodora Bonkob     Answered On: May 05

why not simply type: javac prog1.java and everything will work fine.

I mean, it works fine  unless you don't use a very specific java  compiler .

By the why, there are no dump questions, only dumb answers.
If not you may switch to C++ .

 
Answer #8    Answered By: Della Simpson     Answered On: May 05

the class  is actually called Prog1 - with the second call it's probably
looking for a file  of the class Prog1.class
in the classpath (probably something like Prog1/class.class)- which
would be the class 'class' in the package Prog1
I think there is a restriction on naming a class 'class'........

 
Answer #9    Answered By: Devrim Yilmaz     Answered On: May 05

It sounds like the java  interpeter adds .class to
what ever name you pass it, so a class  named class (or
<something>.class) would confuse it. But a class named Class does
not.(I tried it.) Which is something that annoys me about java case
sensitivity.

 
Answer #10    Answered By: Ella Brown     Answered On: May 05

here is a class  called Class in java.lang, which is generally used for
reflection.
Every class has a member of type Class (which is created by the classloader),
you can (for example) use it like this - this.getClass().getName()
will give you the full classname of the class you use it in.
Regarding the case sensitivity - it's something you get used to.
I tend to use it by naming variables as the classname with a lowercase first
character, as in Fractal fractal = new LambdaFn(), which makes clearer what
type a variable is (often useful in debugging).

 
Didn't find what you were looking for? Find more on Dumb question? Or get search suggestion and latest updates.




Tagged: