Help with -classpath and packages

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • dlarsson

    #1

    Help with -classpath and packages


    Okay folks, I thought I was doing something very, very simple,
    but I cannot seem to get this to work at all.

    Can anyone identify what I am doing wrong here-?
    _______________ _______________ ___


    1. I have a class called "Car" that defines a package with
    the statement: package com.example.ren talcar;

    2. Then I have a test program called TestCar that imports
    this package as follows:
    import com.example.ren talcar.*;

    3. Now, I then first compile the Car package as follows
    javac -classpath . -d . Car.java

    This succesfully compiles Car.java and places the Car.class file
    in the subdirectory ==> com/example/rentalcar relative to
    my current working directory (where both source files originate
    from).

    So, I have a current directory and then the generated
    com/example/rentalcar subdirectory with the "Car.class"
    file placed there - by this compile statement.
    So far so good.

    4. Okay, now I want to compile the little test program
    that has the corresponding import com.example.ren talcar.*;
    statement in it and uses the "Car" class.

    ---
    So I try this:

    javac -classpath . TestCar.java

    I get the error:
    TestCar.java:8: cannot access Car
    bad class file: .\Car.java
    file does not contain class Car
    Please remove or make sure it appears in the correct
    subdirectory of the classpath.
    ---

    If I try to use the fully qualified path to my current directory

    javac -classpath C:\Derek\java TestCar.java

    I still get the same error:
    TestCar.java:8: cannot access Car
    bad class file: .\Car.java
    file does not contain class Car
    Please remove or make sure it appears in the correct
    subdirectory of the classpath.
    ---

    Now if I try to use the fully qualified path to the package itself (I
    know this shouldn't work)

    javac -classpath C:\Derek\java\c om\example\rent alcar TestCar.java

    I get the error:
    TestCar.java:2: package com.example.ren talcar does not exist
    import com.example.ren talcar.*;
    TestCar.java:8: cannot access Car
    bad class file: C:\Derek\java\c om\example\rent alcar\Car.class
    class file contains wrong class: com.example.ren talcar.Car

    ---

    Just what do I need to do-?

    What the heck do I have to set classpath to in order to get the java
    compilier to resolve the import com.example.ren talcar.* statement
    properly
    in this TestCar program-?

    I can't seem to feed anything to -classpath to make it work.....

    What did I do wrong (and how do compile TestCar) -?


    I appreciate all helpful responses .....


    - Derek

    EMail: derek_larsson@c omcast.net






  • dlarsson

    #2
    Re: Help with -classpath and packages


    This should be so simple .. but I am
    really baffled why I can't get this to work ....

    - Derek

    EMail: derek_larsson@c omcast.net



    Comment

    • Sigmund Hansen

      #3
      Re: Help with -classpath and packages

      dlarsson wrote:[color=blue]
      > Okay folks, I thought I was doing something very, very simple,
      > but I cannot seem to get this to work at all.
      >
      > Can anyone identify what I am doing wrong here-?[/color]

      *snip*
      [color=blue]
      >
      > ---
      > So I try this:
      >
      > javac -classpath . TestCar.java
      >
      > I get the error:
      > TestCar.java:8: cannot access Car
      > bad class file: .\Car.java
      > file does not contain class Car
      > Please remove or make sure it appears in the correct
      > subdirectory of the classpath.
      > ---[/color]

      Well, the Car.java file should really be placed inside a directory
      called com/example/rentalcar
      This is why it tells you to place it in the correct subdirectory,
      or at least I think it is.
      Usually you'd have a structure sort of like this:

      projectdir/src/com/example/rentalcar/Car.java

      projectdir/build/com/example/rentalcar/Car.class

      When compiling from the src dir, just set the -d to ../build (backslash
      on windows, obviously).
      If this doesn't work then I don't know,
      I'd recommend you use NetBeans or Eclipse to do your projects though, it
      would simplify these tasks a lot.
      [color=blue]
      > Now if I try to use the fully qualified path to the package itself (I
      > know this shouldn't work)
      >
      > javac -classpath C:\Derek\java\c om\example\rent alcar TestCar.java
      >
      > I get the error:
      > TestCar.java:2: package com.example.ren talcar does not exist
      > import com.example.ren talcar.*;
      > TestCar.java:8: cannot access Car
      > bad class file: C:\Derek\java\c om\example\rent alcar\Car.class
      > class file contains wrong class: com.example.ren talcar.Car[/color]

      Obviously because there's no subdirectory com/example/rentalcar

      Comment

      • Fergus Gibson

        #4
        Re: Help with -classpath and packages

        Sigmund Hansen wrote:[color=blue]
        > Well, the Car.java file should really be placed inside a directory
        > called com/example/rentalcar
        > This is why it tells you to place it in the correct subdirectory,
        > or at least I think it is.[/color]

        Yes, Sigmund, I think you've got it. The compiler can't find the Car.java
        file where it expects it in order to ensure that it's being used properly by
        TestCar.java.

        Derek, it's important to conceive of the -cp and -d switches as referring to
        the root of a directory tree. When you're using packages, you need to have
        a full path representing the package (com/example/rentalcar/) as Sigmund has
        said. Java is organized hierarchially, and it applies this to both its
        source file and its compiled files.

        [color=blue]
        > I'd recommend you use NetBeans or Eclipse to do your projects though,
        > it would simplify these tasks a lot.[/color]

        I tried to get into using an IDE, but I never could. I don't like how they
        put each project into its own directory. I use a text editor and two
        hierarchies, one under "source" and one under "classes". This way I can
        import anything from anywhere in my tree with a simple import statement,
        which I could find any easy way to do in an IDE due to the class and source
        files being distributed into many different project dirs.

        I always felt like I was missing something when I tried to use them, like
        there had to be some easy way to do it...


        Comment

        • Oliver Wong

          #5
          Re: Help with -classpath and packages

          [crossposted to comp.lang.java. programmer, because apparently the
          comp.lang.java newsgroup is deprecated]

          "Fergus Gibson" <nospam@noemail .com> wrote in message
          news:TNsog.1037 23$Mn5.10920@pd 7tw3no...[color=blue]
          >
          > I tried to get into using an IDE, but I never could. I don't like how
          > they put each project into its own directory. I use a text editor and two
          > hierarchies, one under "source" and one under "classes". This way I can
          > import anything from anywhere in my tree with a simple import statement,
          > which I could find any easy way to do in an IDE due to the class and
          > source files being distributed into many different project dirs.
          >
          > I always felt like I was missing something when I tried to use them, like
          > there had to be some easy way to do it...[/color]

          I guess what you're missing is that generally, you don't want to have
          circular dependencies between projects going all over the place.

          For example, you might have one project for each application and/or
          library, and the applications won't directly be accessing each other's class
          files. If they communicate in any way, it might be via some sort of plugin
          architecture (e.g. one project is a plugin for another project which is the
          "main" application). These application projects might have references to a
          library project, but the library would never need to know or access any
          class files defined within the applications which use that library.

          - Oliver

          Comment

          • Sigmund Hansen

            #6
            Re: Help with -classpath and packages

            Fergus Gibson wrote:
            [color=blue]
            > I tried to get into using an IDE, but I never could. I don't like how they
            > put each project into its own directory. I use a text editor and two
            > hierarchies, one under "source" and one under "classes". This way I can
            > import anything from anywhere in my tree with a simple import statement,
            > which I could find any easy way to do in an IDE due to the class and source
            > files being distributed into many different project dirs.
            >
            > I always felt like I was missing something when I tried to use them, like
            > there had to be some easy way to do it...
            >
            >[/color]

            Ah, yeah.
            For libraries that you create (I for one am making a game library, with
            common classes that I will be using among several libraries),
            you can add to your list of libraries, then add those libraries to
            projects that use them,
            if you change something in the library, when compiling a project that
            uses it, it will also recompile the library project.

            At least that's how it works in NetBeans, I've used Sun Java Studio
            (later Sun ONE Studio for Java or something, and now NetBeans), and I
            really like it, I hear that Eclipse is better, but NetBeans was always
            free, and I didn't really have much use for anything more fancy. ;)

            In NetBeans I simply go into the project's properties, then to Libraries
            (it doesn't really have to be libraries, but any other package really),
            then I just click Add Project, and all imports are fixed.
            Still, everyone has a preferred way of working, and I only recently have
            had use for this(as I don't really code a lot), and found it very easy
            to set up, now I even have version control in place, with my own
            subversion server (if you want to use svn I recommend that you use NB
            5.5beta over NB 5.0 - way better, although the svn support is not
            perfectly bug free nor completely finished)...

            Comment

            • Fergus Gibson

              #7
              Re: Help with -classpath and packages

              Sigmund Hansen wrote:[color=blue]
              > For libraries that you create (I for one am making a game library,
              > with common classes that I will be using among several libraries),
              > you can add to your list of libraries, then add those libraries to
              > projects that use them,
              > if you change something in the library, when compiling a project that
              > uses it, it will also recompile the library project.[/color]

              This term "library" is a big part of my confusion. There is no concept of a
              "library" intrinsic to Java. I don't know what the IDE implies when it
              calls this thing a "library" and that thing an "applicatio n". Java has
              packages and classes, which I understand quite well.

              I know what the generic terminology means, but I don't know what the IDE
              means when it uses those words. What is a Java library? A utility package
              whose classes are meant to be used by many other packages ("applications" )
              to achieve some common objectives?

              I guess I feel a certain resistance to learning the IDE. I understand the
              language and packages and classes, and I don't want to learn a whole new set
              of semantics. I also balk at becoming totally dependent on the IDE. It
              seems to me there would be no easy to way to compile the "applicatio ns" and
              "libraries" with just the simple javac command-line compiler once they are
              split up into a whole bunch of isolated directory trees.

              [color=blue]
              > At least that's how it works in NetBeans, I've used Sun Java Studio
              > (later Sun ONE Studio for Java or something, and now NetBeans), and I
              > really like it, I hear that Eclipse is better, but NetBeans was always
              > free, and I didn't really have much use for anything more fancy. ;)[/color]

              I used Sun ONE Studio for Java or whatever exactly it was called and
              NetBeans too. I found them off-putting. I guess I want an IDE the uses Java
              semantics and approaches rather than trying to look, act, and sound like
              Visual Studio...


              Comment

              • Oliver Wong

                #8
                Re: Help with -classpath and packages


                "Fergus Gibson" <nospam@noemail .comwrote in message
                news:OU0pg.1097 99$Mn5.2326@pd7 tw3no...
                >
                This term "library" is a big part of my confusion. There is no concept of
                a "library" intrinsic to Java. I don't know what the IDE implies when it
                calls this thing a "library" and that thing an "applicatio n". Java has
                packages and classes, which I understand quite well.
                >
                I know what the generic terminology means, but I don't know what the IDE
                means when it uses those words. What is a Java library? A utility
                package whose classes are meant to be used by many other packages
                ("applications" ) to achieve some common objectives?
                I don't think the IDE applies any special behaviour to what one might
                call a "library" versus what one might call an "applicatio n".

                I guess the idea is that each project will eventually be distributed as
                its own JARs. Some of these JARs have an entry point, and so when you try to
                run them (via "java -jar whatever.jar"), something will actually happen.
                These are "applicatio ns". Other jars just contain classes for applications
                to use, and you can't actually "run" them. Those would be the libraries.

                But again, you don't mark a project as being an application or a library
                within Eclipse (I don't know about the other IDEs). It's just a label I
                apply when speaking to other humans to communicate the intent of the
                project.
                >
                I guess I feel a certain resistance to learning the IDE. I understand the
                language and packages and classes, and I don't want to learn a whole new
                set of semantics. I also balk at becoming totally dependent on the IDE.
                It seems to me there would be no easy to way to compile the "applicatio ns"
                and "libraries" with just the simple javac command-line compiler once they
                are split up into a whole bunch of isolated directory trees.
                If you're working from the command line, I think you'd usually use a
                build tool like "make" or "ant".

                - Oliver

                Comment

                Working...