Package help

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

  • 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

  • [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
    :
    :



  • 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

  • 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
    :
    :

  • : 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


  • 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
    :
    :
    :

  • : 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


  • : 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

  • 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
    :
    :

  • : 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

  • The fact that my current working directory wasn't in my classpath was because I took it out while trying to fix this problem (it was a good thought though).

    Oddly, I was playing around a bit and did get it to work via the following syntax...

    java bin2.hw

    Is this normal syntax when working with packages?

    : : 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
    :
    :

  • : Oddly, I was playing around a bit and did get it to work via the following syntax...
    : java bin2.hw
    : Is this normal syntax when working with packages?

    Well, it looks like a class named hw is in a directory called bin2 BUT you're actually not in bin2 but in an other directory (bin?) that has bin2 as it's subdirectory.

    Like this:
    [blue]c:KonradsCoolProgsin> [italic][size=1]you are here[/size][/italic][/blue]
    while the actuall class is here:
    [blue]c:KonradsCoolProgsinin2hw.class[/blue]

    Is that somethig you recognize?


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

  • : The fact that my current working directory wasn't in my classpath was because I took it out while trying to fix this problem (it was a good thought though).
    :
    : Oddly, I was playing around a bit and did get it to work via the following syntax...
    :
    : java bin2.hw
    :
    : Is this normal syntax when working with packages?
    :

    It must be. I got it to work by classpathing to the directory above the bin2 directory then used bin2.hw

    java -classpath c:jdk1.4in bin2.hw


  • : It must be. I got it to work by classpathing to the directory above the bin2 directory then used bin2.hw

    What i don't understand is this. You had "." in your classpath before. Now, if you are in the actuall directory (which is at that point reffered to as ".") and you don't get it working, then how come you get it to work if you classpath the very same directory (allthough reffered to as a full path)?

    It sounds suspicious...

    Anyway - glad to hear you made that sucker (computer) obey :-)!


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

  • The layout of my dir structure is as follows:

    c:jdk1.4inin2hw.class

    However, whether I run the java command from the bin or bin2 directory, the syntax to run that command appears to have to be as I stated below. I could understand if it was only when I ran the command from the bin directory, but the fact that I also need to state the package name when I am in bin2 confuses me.

    : : The fact that my current working directory wasn't in my classpath was because I took it out while trying to fix this problem (it was a good thought though).
    : :
    : : Oddly, I was playing around a bit and did get it to work via the following syntax...
    : :
    : : java bin2.hw
    : :
    : : Is this normal syntax when working with packages?
    : :
    :
    : It must be. I got it to work by classpathing to the directory above the bin2 directory then used bin2.hw
    :
    : java -classpath c:jdk1.4in bin2.hw
    :
    :
    :

Sign In or Register to comment.

Howdy, Stranger!

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

Categories