I am having trouble with packages. I am new to java and learning to compile using javac on command line.
I can compile a simple java file such as HelloWorld and run it.... I can also compile multiple source files from javac command and run, using this example:
Java Pong Tutorial
I downloaded the 3 source files at the end of the article(Pong.Java, Ball.java, Paddle.java), I can compile them using simple commands like "javac -d ./classes/ ./com/test/pong/*.java" with the files in the appropriate folders. I can then run it just fine (using "java Pong")
My problem occurs if I try to add "package com.test.pong;" to the files at the start..... after compiling, if I try to run the program as I did before, I will get the error:
Exception in thread "main" java.lang.NoclassDefFoundError: Pong (wrong name: com/test/pong/Pong)
at java.lang.ClassLoader.defineClass1(NativeMethod)
^^^... lots of lines like this
Could not find the main class Pong. program will exit.
I thought maybe I had to specify classpath to fix this, so I tried:
java -classpath c:/Xdev3/Pong com.test.pong
However this gives a similar error to above..... also gives the same error if I mispell this path... but also when it is the right path.
I have read a few articles and scanned forums, and learnt a bit about packages, for example using classes from other packages by importing the namespace+classname eg:
import java.awt.event, lets you use all classes from that package, and
import java.awt.event.ActionEvent, imports just that class from that package.
And how if I don't specify a namespace, it ends up in an unamed package -which I guess happend with my original working version of Pong....... I have so far found nothing to indicate how to fix my error though. I have asked people but I just get "read about packages" answer... I have been reading about packages all day but still have not found the answer I need.... I am sure it is something small and simple but key that I have missed. There are only 3 java files, all in the same folder... I can compile and run them until I add the "package com.test.pong;" to the start of each of them. As I can see it, this should simply define these files as part of that named package, so that other files could refer to them and use the classes within this one.... but instead it stops this very one from working itself.
If anyone has the answer, or can point out a tutorial/article with the answer, I would appreciate it very much. Thank you.