multiple source files

C

conrad

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.
 
D

Donkey Hot

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.

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/
 
A

Arne Vajhøj

conrad said:
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
 
L

Lew

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).
 
M

Mark Space

conrad said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top