Results 1 to 3 of 3
  1. #1
    droidtech1 is offline Member
    Join Date
    Jul 2011
    Posts
    6
    Rep Power
    0

    Default 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.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default

    .NoclassDefFoundError: Pong (wrong name: com/test/pong/Pong)
    To execute this class you need to be in the folder that contains the com folder. Then execute:
    java com.test.pong.Pong

    Cross posted at http://www.javaprogrammingforums.com...g-problem.html
    Last edited by Norm; 07-29-2011 at 06:59 PM.

  3. #3
    droidtech1 is offline Member
    Join Date
    Jul 2011
    Posts
    6
    Rep Power
    0

    Default

    Thanks. It works perfectly now. I knew it was something simple like that.
    Sorry for the double post, didn't realise it was the same forum.

Similar Threads

  1. Textpad troubles
    By Sthassaris in forum Other IDEs
    Replies: 3
    Last Post: 06-03-2011, 10:38 PM
  2. Clock Troubles
    By brycepower1 in forum New To Java
    Replies: 55
    Last Post: 05-12-2011, 01:33 PM
  3. run package inside anthor package
    By AhmedAdel in forum AWT / Swing
    Replies: 4
    Last Post: 04-20-2010, 12:52 PM
  4. Replies: 7
    Last Post: 11-09-2009, 08:51 AM
  5. subclass troubles
    By xf021209 in forum New To Java
    Replies: 12
    Last Post: 04-21-2009, 12:46 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •