Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

help me in java

0 views
Skip to first unread message

brainbarshan

unread,
Jul 17, 2009, 10:39:42 PM7/17/09
to
hi! i am new in java. i am just trying to learn it.
but i have got a problem. please anyone help me:
here is the code:

public class ap1{
public static void main(String[] args){
System.out.println("Hello there!");
}
}

i save it as ap1.java in c: drive.
when i compile it with java : javac ap1.java
it is compiled and a class file named ap1.class is created in c:
drive.
but when i am trying to run the class file by java ap1.class
it shows following errors:

Exception in thread "main" java.lang.NoClassDefFoundError: ap1
Caused by: java.lang.ClassNotFoundException: ap1
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:
301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:
320)
Could not find the main class: ap1. Program will exit.

both java.exe and javac.exe are in c:\program files\java
\jdk1.6.0_14\bin

i set environmen variables as :
CLASSPATH -- C:\Program Files\Java\jdk1.6.0_14\bin
PATH - C:\Program Files\Java\jdk1.6.0_14\bin
in control panel--> system--> advanced-->Environment Variables

I cant get what is getting wrong....please help me out

Lew

unread,
Jul 17, 2009, 11:04:50 PM7/17/09
to
brainbarshan wrote:
> hi! i [sic] am new in java [sic]. i am just trying to learn it.

The newsgroup comp.lang.java.help is perfect for this kind of question.
comp.lang.java.advocacy is for the expression of in favor of or against Java,
for or against parts of the language, for or against new features, or for or
against related products or projects.

> but when i am trying to run the class file by java ap1.class

The "java" command takes a class as an argument, not a file. Class names are
similar to file names, but without the ".class" part, so you should type:

java ap1

There are coding conventions for Java that one should begin class or other
interface names with an upper-case letter, and (most) variable and method
names with a lower-case letter. Use single or compound words for names, such
as "class SomethingNifty"; each compound word part other than the first begins
with an upper-case letter for both classes and variables or methods. So you
should get in the habit, using your example, of naming a class like:

public class Ap1

and you would invoke it with

java Ap1

Later you will learn about packages, which would give you a command like:

java com.lewscanon.SomethingNifty

Read the material at <http://java.sun.com/>, especially the tutorials.

Please follow up at comp.lang.java.help . Another good newsgroup is
comp.lang.java.programmer but it is somewhat more advanced.

Although the command to invoke a Java program is "java", the name of the
language is "Java". The language itself is very picky about upper- and
lower-case spelling, so it's a good idea to get in the habit of rigor about
letter case generally.

--
Lew

brainbarshan

unread,
Jul 17, 2009, 11:23:39 PM7/17/09
to
On Jul 18, 8:04 am, Lew <no...@lewscanon.com> wrote:
> Lew wrote

> The "java" command takes a class as an argument, not a file.  Class names are
> similar to file names, but without the ".class" part, so you should type:
>
>    java ap1

but I have tried with java ap1.
it shows the same error.

Rajjo

unread,
Jul 19, 2009, 1:45:10 AM7/19/09
to


Set this command in command prompt and try

set classpath=%classpath%;.;

Lew

unread,
Jul 19, 2009, 11:57:05 AM7/19/09
to
brainbarshan wrote:
>> but I have tried with java ap1.
>> it shows the same error.

Rajjo wrote:
> Set this command in command prompt and try
>
> set classpath=%classpath%;.;

The environment variable is "CLASSPATH", not "classpath". The syntax you sow
is for Windows only; it will not work in other OSes.

It is far better not to set CLASSPATH at all. If no CLASSPATH environment
variable is set, it defaults to "." (the current working directory), so this
step should not be necessary anyway.

The best way to set the class path is with the "-classpath" or "-cp" option to
the "java" command. Again, this should not be necessary, since the default is
"." anyway, but the OP can try

java -cp . Apl

--
Lew

Roedy Green

unread,
Jul 20, 2009, 11:51:55 AM7/20/09
to
On Fri, 17 Jul 2009 19:39:42 -0700 (PDT), brainbarshan
<brainb...@gmail.com> wrote, quoted or indirectly quoted someone
who said :

> java ap1.class

just

java.exe Ap1

no .class. It is illogical. javac.exe wants .java. but java.exe does
not want .class. Class names should start with a capital letter.

see http://mindprod.com/jgloss/helloworld.html


--
Roedy Green Canadian Mind Products
http://mindprod.com

"The industrial civilisation is based on the consumption of energy resources that are inherently limited in quantity, and that are about to become scarce. When they do, competition for what remains will trigger dramatic economic and geopolitical events; in the end, it may be impossible for even a single nation to sustain industrialism as we have know it in the twentieth century."
~ Richard Heinberg, The Party�s Over: Oil, War, and the Fate of Industrial Societies

Roedy Green

unread,
Jul 20, 2009, 11:53:05 AM7/20/09
to
On Fri, 17 Jul 2009 19:39:42 -0700 (PDT), brainbarshan
<brainb...@gmail.com> wrote, quoted or indirectly quoted someone
who said :

>hi! i am new in java. i am just trying to learn it.


>but i have got a problem. please anyone help me:
>here is the code:

You will have better luck with such questions in com.lang.java.help

see http://mindprod.com/jgloss/newsgroups.html
for what each group is best used for.

Roedy Green

unread,
Sep 10, 2009, 2:38:47 PM9/10/09
to
On Fri, 17 Jul 2009 19:39:42 -0700 (PDT), brainbarshan
<brainb...@gmail.com> wrote, quoted or indirectly quoted someone
who said :

>


> Exception in thread "main" java.lang.NoClassDefFoundError: ap1

see http://mindprod.com/jgloss/helloworld.html
http://mindprod.com/jgloss/runerrormessages.html#NOCLASSDEFFOUNDERROR


--
Roedy Green Canadian Mind Products
http://mindprod.com

"The coolest thing to do with your data will be thought of by someone else."
~ Rufus Pollock (born: 1978 age: 31) in Talk.

0 new messages