compiling multiple packages from the command line?

Discussion in 'Java' started by nooneinparticular314159@yahoo.com, May 4, 2006.

  1. Guest

    I usually do my java development inside an IDE. But I need to compile
    my code from the Windows command line. I have two packages:

    ProjectName\src\com\CodeSupplier\CodeFolder, and
    ProjectName\src\MyProject

    The program uses java files from both packages.

    How do I compile this? When I try, I just get errors, because javac
    doesn't know where the other package is. (From Netbeans, this works.)

    Thanks!
    , May 4, 2006
    #1
    1. Advertising

  2. Rhino Guest

    <> wrote in message
    news:...
    >I usually do my java development inside an IDE. But I need to compile
    > my code from the Windows command line. I have two packages:
    >
    > ProjectName\src\com\CodeSupplier\CodeFolder, and
    > ProjectName\src\MyProject
    >
    > The program uses java files from both packages.
    >
    > How do I compile this? When I try, I just get errors, because javac
    > doesn't know where the other package is. (From Netbeans, this works.)
    >

    I don't think I ever did this myself but I recall reading this question in
    the old Java FAQ. I'm certain that the answer said you had to compile them
    both at the same time. I think the command line needs to look like this; I'm
    not certain this is 100% right but it should be pretty close:

    javac my\com\foo.java my\othercom\bar.java

    I also seem to recall that if the source files are in the same directory,
    you can do a:

    javac *.java

    and all source files in the directory will get compiled. Again, I'm not
    certain of the exact syntax but I think it is pretty close to what I have
    given.

    --
    Rhino
    Rhino, May 4, 2006
    #2
    1. Advertising

  3. Carl Guest

    wrote:
    > I usually do my java development inside an IDE. But I need to compile
    > my code from the Windows command line. I have two packages:
    >
    > ProjectName\src\com\CodeSupplier\CodeFolder, and
    > ProjectName\src\MyProject
    >
    > The program uses java files from both packages.
    >
    > How do I compile this? When I try, I just get errors, because javac
    > doesn't know where the other package is. (From Netbeans, this works.)
    >
    > Thanks!


    Hello,

    You are able to tell javac where to find you source files using the
    -sourcepath flag. From the docs (
    http://java.sun.com/j2se/1.4.2/docs/tooldocs/solaris/javac.html ):

    "-sourcepath sourcepath Specify the source code path to search for
    class or interface definitions. As with the user class path, source
    path entries are separated by colons :)) and can be directories, JAR
    archives, or ZIP archives. If packages are used, the local path name
    within the directory or archive must reflect the package name.

    Note that classes found through the classpath are subject to
    automatic recompilation if their sources are found."

    I hope that helps,
    Carl.
    Carl, May 4, 2006
    #3
  4. wrote:
    > I usually do my java development inside an IDE. But I need to compile
    > my code from the Windows command line. I have two packages:
    >
    > ProjectName\src\com\CodeSupplier\CodeFolder, and
    > ProjectName\src\MyProject
    >
    > The program uses java files from both packages.
    >
    > How do I compile this? When I try, I just get errors, because javac
    > doesn't know where the other package is. (From Netbeans, this works.)
    >
    > Thanks!
    >


    The package names of your classes need to match the directory structure.
    Also you need to be in a directory above the top of your package
    directory to compile the files. So if you have a class:

    /com/knutejohnson/programs/MyProgram.java

    and a class:

    /com/knutejohnson/components/MyComponent.java

    you need to be in the / directory and enter:

    javac com/knutejohnson/programs/MyProgram.java

    This will compile both files and then you can run the program with:

    java com.knutejohnson.programs.MyProgram

    This is one of the most difficult subjects to explain and understand
    when you first start using packages. It is only exceeded by CLASSPATH.
    Using jar files with packages is done similarly.

    --

    Knute Johnson
    email s/nospam/knute/
    Knute Johnson, May 5, 2006
    #4
  5. ramakrishna Guest

    You can do this by using -sourcepath directive in javac

    But I need clarification regarding this one

    javac -sourcepath com/*.java:com.thing/*.java:com.thing.network/*.java
    it will work nice

    but in my current project i have some many folders and i need to
    complie the all java file.

    Is there any option regarding this one to compile all the files
    including subdirectores.

    Thank u
    ramakrishna, May 5, 2006
    #5
  6. wrote:
    > I usually do my java development inside an IDE. But I need to compile
    > my code from the Windows command line. I have two packages:
    >
    > ProjectName\src\com\CodeSupplier\CodeFolder, and
    > ProjectName\src\MyProject
    >
    > The program uses java files from both packages.
    >
    > How do I compile this? When I try, I just get errors, because javac
    > doesn't know where the other package is. (From Netbeans, this works.)
    >
    > Thanks!
    >

    Have you considered using a build tool like apache ant? If you're going
    to be doing a lot of this then the extra effort to define the build
    configuration will be worth the pay off.

    --
    TechBookReport Java http://www.techbookreport.com/JavaIndex.html
    TechBookReport, May 5, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David Rainsford

    Compiling from the command line

    David Rainsford, May 4, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    635
    Tarundeep Singh Kalra
    May 6, 2004
  2. Paul  Smith
    Replies:
    0
    Views:
    723
    Paul Smith
    Nov 18, 2003
  3. enjoylife_95135
    Replies:
    2
    Views:
    330
    enjoylife_95135
    Aug 15, 2004
  4. David Lyon
    Replies:
    19
    Views:
    561
    David Lyon
    Apr 24, 2009
  5. Ian
    Replies:
    3
    Views:
    159
Loading...

Share This Page