Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: package config problem?

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default package config problem?

    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.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: package config problem?

    Cross posted at package configuration troubles

Similar Threads

  1. [SOLVED] applets in package
    By dabdi in forum Java Theory & Questions
    Replies: 5
    Last Post: June 14th, 2011, 08:04 PM
  2. Package confusion
    By caesius in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 21st, 2011, 11:08 PM
  3. Is it possible to package Java
    By UnderWater2 in forum Java Theory & Questions
    Replies: 1
    Last Post: November 2nd, 2010, 01:31 AM
  4. creating a package
    By x3rubiachica3x in forum Object Oriented Programming
    Replies: 6
    Last Post: September 26th, 2010, 11:10 PM
  5. [SOLVED] Finding acm package
    By javapenguin in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 12th, 2010, 11:50 AM