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
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:
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.
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.
On Jul 18, 8:23 am, brainbarshan <brainbars...@gmail.com> wrote:
> 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.
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
"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
"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