Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Package help

froggsfroggs Member Posts: 7
I'm trying to figure out how to use packages and I'm getting an error that is likely simple to fix, but I can't seem to figure out my mistake. Using the simple helloworld program below, it compiles and runs fine:

class hw {
public static void main(String[] args) {
System.out.println("Hello World!"); // Display "Hello World!"
}
}


However, if I slightly edit the program to:

package bin2;
class hw {
public static void main(String[] args) {
System.out.println("Hello World!"); // Display "Hello World!"
}
}

it compiles fine , but I get the following runtime error message:

Exception in thread "main" java.lang.NoClassDefFoundError: hw (wrong name: bin2/hw)

My classpath is currently set to c:jdkin and hw.java and hw.class are located in c:jdkinin2. As far as I can tell, the package bin2 should be valid using my example. Any ideas what I'm doing wrong? Thanx.
«1

Comments

  • chamsterchamster Member Posts: 662
    What exactly do you type to run the program? Or do you use IDE?


    Kind Regards
    Konrad
    ----------------------------
    (+46/0) 708-70 73 92
    chamster@home.se
    http://konrads.webbsida.com

  • froggsfroggs Member Posts: 7
    [b][red]This message was edited by froggs at 2002-9-23 7:38:45[/red][/b][hr]
    I type:

    javac hw.java

    to compile and

    java hw

    to run while in the bin2 directory.

    : What exactly do you type to run the program? Or do you use IDE?
    :
    :
    : Kind Regards
    : Konrad
    : ----------------------------
    : (+46/0) 708-70 73 92
    : chamster@home.se
    : http://konrads.webbsida.com
    :
    :



  • chamsterchamster Member Posts: 662
    Oh, i think i know what that is. Try this line when running the program instead:
    [code]
    java -classpath . hw
    [/code]

    And if it will work then i'll be more than happy to explain to you what the darn OS bothered you with :-). It's not your fault, more like a huge misstake about what "current directory" is supposed to mean. I blame Sun/Unix for it...


    Kind Regards
    Konrad
    ----------------------------
    (+46/0) 708-70 73 92
    chamster@home.se
    http://konrads.webbsida.com

  • froggsfroggs Member Posts: 7
    I think I know why you had me try this...specify the working directory...but it didn't seem to do the trick! Any other ideas? If not, thanx for trying!!

    : Oh, i think i know what that is. Try this line when running the program instead:
    : [code]
    : java -classpath . hw
    : [/code]
    :
    : And if it will work then i'll be more than happy to explain to you what the darn OS bothered you with :-). It's not your fault, more like a huge misstake about what "current directory" is supposed to mean. I blame Sun/Unix for it...
    :
    :
    : Kind Regards
    : Konrad
    : ----------------------------
    : (+46/0) 708-70 73 92
    : chamster@home.se
    : http://konrads.webbsida.com
    :
    :

  • beancounterbeancounter Member Posts: 88
    : I think I know why you had me try this...specify the working directory...but it didn't seem to do the trick! Any other ideas? If not, thanx for trying!!
    :
    : : Oh, i think i know what that is. Try this line when running the program instead:
    : : [code]
    : : java -classpath . hw
    : : [/code]
    : :
    : : And if it will work then i'll be more than happy to explain to you what the darn OS bothered you with :-). It's not your fault, more like a huge misstake about what "current directory" is supposed to mean. I blame Sun/Unix for it...
    : :
    : :
    : : Kind Regards
    : : Konrad
    : : ----------------------------
    : : (+46/0) 708-70 73 92
    : : chamster@home.se
    : : http://konrads.webbsida.com
    : :
    : :
    :
    :
    Hello,

    I think you need to make your hw class in the bin2 package public.

    You can then launch it using the methods mentioned earlier.

    I think this will work.

    Hope this helps


  • froggsfroggs Member Posts: 7
    I actually had tried this before I posted the question with no luck...

    public class hw

    I gave it another shot with the other runtime option presented and still no go!!


    : : :
    : :
    : :
    : Hello,
    :
    : I think you need to make your hw class in the bin2 package public.
    :
    : You can then launch it using the methods mentioned earlier.
    :
    : I think this will work.
    :
    : Hope this helps
    :
    :
    :

  • beancounterbeancounter Member Posts: 88
    : I actually had tried this before I posted the question with no luck...
    :
    : public class hw
    :
    : I gave it another shot with the other runtime option presented and still no go!!
    :
    :
    : : : :
    : : :
    : : :
    : : Hello,
    : :
    : : I think you need to make your hw class in the bin2 package public.
    : :
    : : You can then launch it using the methods mentioned earlier.
    : :
    : : I think this will work.
    : :
    : : Hope this helps
    : :
    : :
    : :
    :
    :
    Hmm,

    That's very strange I can get it to run on my windows box from the command prompt using:
    java -classpath c:jdk1.4inin2 hw


  • chamsterchamster Member Posts: 662
    : I think I know why you had me try this...specify the working directory...but it didn't seem to do the trick! Any other ideas? If not, thanx for trying!!

    Damn! That tip works like 99.9% of time... What do you get if you type
    [b]set classpath[/b] in the console window? (I'm hunting for ideas now, so it may be a little not to-the-point)


    Kind Regards
    Konrad
    ----------------------------
    (+46/0) 708-70 73 92
    chamster@home.se
    http://konrads.webbsida.com

  • froggsfroggs Member Posts: 7
    classpath=c:j2sdk1.4.0_02in;c:j2sdk1.4.0_02inin2


    : : I think I know why you had me try this...specify the working directory...but it didn't seem to do the trick! Any other ideas? If not, thanx for trying!!
    :
    : Damn! That tip works like 99.9% of time... What do you get if you type
    : [b]set classpath[/b] in the console window? (I'm hunting for ideas now, so it may be a little not to-the-point)
    :
    :
    : Kind Regards
    : Konrad
    : ----------------------------
    : (+46/0) 708-70 73 92
    : chamster@home.se
    : http://konrads.webbsida.com
    :
    :

  • chamsterchamster Member Posts: 662
    : classpath=c:j2sdk1.4.0_02in;c:j2sdk1.4.0_02inin2

    OK, i'm surprised that my tip didn't work for you. I still think you should add "." to your classpath. You're going to need to that either way so...

    You classpath should be like this:
    classpath=.;c:j2sdk1.4.0_02in;c:j2sdk1.4.0_02inin2

    Try this and let us know if you get rid of errors and what other errors you managed to receive :-).


    Kind Regards
    Konrad
    ----------------------------
    (+46/0) 708-70 73 92
    chamster@home.se
    http://konrads.webbsida.com

«1
Sign In or Register to comment.