multiple source files

Discussion in 'Java' started by conrad, Mar 8, 2008.

  1. conrad

    conrad Guest

    Could someone point me in the right direction
    in how one should compile a program in
    java with multiple source files? In C, or
    C++ I would have separate translation units
    that serve some particular purpose.
    I would then compile those translation
    units into object files and then supply
    all the object files to the compiler
    to generate a single output file.
    One of my translation units would have
    main in it. Now, I'm wondering how this
    is approached in java because I really don't
    want a single class with many unrelated
    methods.

    --
    conrad
     
    conrad, Mar 8, 2008
    #1
    1. Advertising

  2. conrad

    Donkey Hot Guest

    conrad <> wrote in news:a0bae9ea-f8ae-4bb7-b3e7-
    :

    > Could someone point me in the right direction
    > in how one should compile a program in
    > java with multiple source files? In C, or
    > C++ I would have separate translation units
    > that serve some particular purpose.
    > I would then compile those translation
    > units into object files and then supply
    > all the object files to the compiler
    > to generate a single output file.
    > One of my translation units would have
    > main in it. Now, I'm wondering how this
    > is approached in java because I really don't
    > want a single class with many unrelated
    > methods.
    >
    > --
    > conrad
    >


    In C or C++ you first compile to object files, and then link them together
    to get an executable.

    In Java you just compile the source files to class files, and they are
    immediately executable. There is no need for "linking" them together.

    You start the application with the class having the main() and java just
    finds the other class files when it needs them.

    That way, your application contains lots (tens, hundreds, thousands) of
    ..class files on the disk. Painful.

    You can pack the application into a .jar file - which is actually a .zip
    having the classes in it. There may also be a special MANIFEST file in it,
    which may contain the main class name in it, so the .jar can be handled as
    a real executable which starts with a double click.

    You might want to spend some time with the Java tutorials on

    http://java.sun.com/docs/books/tutorial/
     
    Donkey Hot, Mar 8, 2008
    #2
    1. Advertising

  3. conrad

    Arne Vajhøj Guest

    conrad wrote:
    > Could someone point me in the right direction
    > in how one should compile a program in
    > java with multiple source files? In C, or
    > C++ I would have separate translation units
    > that serve some particular purpose.
    > I would then compile those translation
    > units into object files and then supply
    > all the object files to the compiler
    > to generate a single output file.
    > One of my translation units would have
    > main in it. Now, I'm wondering how this
    > is approached in java because I really don't
    > want a single class with many unrelated
    > methods.


    Each .java file becomes a .class file.

    You can "bundle" many .class files in a .jar file.

    I think that is what you want to use.

    Arne
     
    Arne Vajhøj, Mar 8, 2008
    #3
  4. conrad

    Lew Guest

    conrad wrote:
    >> Could someone point me in the right direction
    >> in how one should compile a program in
    >> java with multiple source files? In C, or
    >> C++ I would have separate translation units
    >> that serve some particular purpose.
    >> I would then compile those translation
    >> units into object files and then supply
    >> all the object files to the compiler
    >> to generate a single output file.
    >> One of my translation units would have
    >> main in it. Now, I'm wondering how this
    >> is approached in java because I really don't
    >> want a single class with many unrelated
    >> methods.


    Arne Vajhøj wrote:
    > Each .java file becomes a .class file.
    >
    > You can "bundle" many .class files in a .jar file.
    >
    > I think that is what you want to use.


    In addition to the others' advice, which cover the primary points, both the
    "loose" class files and the internal structure of a JAR file follow Java's
    package structures, and strictures.
    <http://java.sun.com/docs/books/tutorial/java/package/index.html>

    Packages define and limit accessibility of classes to each other.
    Semantically, they are namespaces to the language, and object-file (to use C
    parlance) organizers to the deployer. The directory/moredirectory/file
    hierarchy in the file system or JAR corresponds to the
    package.morepackage.class dotted notation of the (non-hierarchical) namespace
    system.

    To carry the C-to-Java analogy forward, a .class is like a .o (.obj), a
    package is like a .a (.lib), and a JAR is like a .out (.exe).

    --
    Lew
     
    Lew, Mar 8, 2008
    #4
  5. conrad

    Mark Space Guest

    conrad wrote:
    > Could someone point me in the right direction
    > in how one should compile a program in
    > java with multiple source files? In C, or
    > C++ I would have separate translation units
    > that serve some particular purpose.
    > I would then compile those translation
    > units into object files and then supply
    > all the object files to the compiler
    > to generate a single output file.
    > One of my translation units would have
    > main in it. Now, I'm wondering how this
    > is approached in java because I really don't
    > want a single class with many unrelated
    > methods.



    Java works almost the same way.

    Source files (translation units) are .java files. They are complied to
    ..class files by the compiler.

    You can do this any way you like. On the command line or with the
    "make" utility would work fine. IDEs are easier, check out NetBeans.
    There is a newer snazzier version of "make" called Ant that is better
    for Java, check it out.

    After you get the .class files, you link the .class files into larger
    execution units called .jar files. .jar files can have additional items
    besides .class files. They can also hold plain old data files (.txt,
    binary, etc.) which can be then accessed as a "resource" by the .jar.
    There are also special data files called resource bundles which have
    special support.

    Finally, there's something vaguely akin to C++ namespaces called
    "packages." They're a little funky. They have language access support
    (like friend classes in C++) and they also have a special directory
    structure. It's pretty easy to get around their access support though,
    you don't even need source code. A bit of an honor system.

    The posts above cover all this stuff a bit better. This is the simple,
    sorta kinda korrect missive that tries to get you the big picture in one
    easy to digest post.

    Good luck.
     
    Mark Space, Mar 8, 2008
    #5
    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. Rita Mathoa

    xsl-fo and multiple source files?

    Rita Mathoa, Jun 26, 2004, in forum: XML
    Replies:
    2
    Views:
    407
    Rita Mathoa
    Jun 28, 2004
  2. Replies:
    4
    Views:
    962
    M.E.Farmer
    Feb 13, 2005
  3. Gerard Kramer
    Replies:
    2
    Views:
    313
    Gerard Kramer
    Jun 17, 2006
  4. Matt
    Replies:
    4
    Views:
    306
    Kenny McCormack
    Jul 11, 2007
  5. Replies:
    17
    Views:
    97
    Rustom Mody
    May 22, 2014
Loading...

Share This Page