Results 1 to 3 of 3
Thread: package configuration troubles
- 07-29-2011, 06:35 PM #1
Member
- Join Date
- Jul 2011
- Posts
- 6
- Rep Power
- 0
package configuration troubles
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:
http://www.giosoft.net/Development/J...-Tutorial.html
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.
- 07-29-2011, 06:56 PM #2.NoclassDefFoundError: Pong (wrong name: com/test/pong/Pong)
java com.test.pong.Pong
Cross posted at http://www.javaprogrammingforums.com...g-problem.htmlLast edited by Norm; 07-29-2011 at 06:59 PM.
- 07-29-2011, 08:35 PM #3
Member
- Join Date
- Jul 2011
- Posts
- 6
- Rep Power
- 0
Similar Threads
-
Textpad troubles
By Sthassaris in forum Other IDEsReplies: 3Last Post: 06-03-2011, 10:38 PM -
Clock Troubles
By brycepower1 in forum New To JavaReplies: 55Last Post: 05-12-2011, 01:33 PM -
run package inside anthor package
By AhmedAdel in forum AWT / SwingReplies: 4Last Post: 04-20-2010, 12:52 PM -
The declared package does not match the expected package
By oneforall in forum EclipseReplies: 7Last Post: 11-09-2009, 08:51 AM -
subclass troubles
By xf021209 in forum New To JavaReplies: 12Last Post: 04-21-2009, 12:46 AM
Bookmarks