File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes How should I organize classes and source on disk? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How should I organize classes and source on disk?" Watch "How should I organize classes and source on disk?" New topic
Author

How should I organize classes and source on disk?

Timothy Chen Allen
Ranch Hand

Joined: Mar 16, 2003
Posts: 161
I would like to know if there is some uniform way to organize my packages in my directory structure. I'm on a Windows machine using javac as my compiler and a text editor (gvim) to create all my .java files.
I am learning how to use packages, and I want to set all of my classes up in the same package hierarchy (org.timallen.foo, org.timallen.bar.utils, etc-- BTW, that really is my domain). How should I set this up on disk?
Keep in mind that I eventually might start using Ant to maintain my classes.
Does any of this change if I decide to distribute my classes in a jar file?
Finally, how do I have to set my classpath to support all of this?
Thanks in advance.


Timothy Chen Allen
Learn Spanish in Washington, DC
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1817

I have everything under one directory (D:\user\java\sources in my case; you can do what you want...). Then, all of my source code goes there. So, for example, if I have a class called "CFile" in a package "com.coolskill.foundation.io", the code is located at:
D:\user\java\sources\com\coolskill\foundation\io\CFile.java
D:\user\java\sources\com\coolskill\foundation\io\CFile.class
When I distribute my classes as .jar files, I simply package them into a jar file.
As far as setting the classpath, on my machine the classpath includes D:\user\java\sources, since that is where all my code is.
On a machine that has your code distributed, you muct include the .jar file in the classpath (eg. "C:\Program Files\CoolApp\libs\Foundation.jar")
--------------------
*Cavaets:
1). I've not used ANT, so I can't say how this setup affects Ant
2). IDEs such as Eclipse do not allow the same directory to be in multiple projects. I use NetBeans (NetBeans/SUN ONE, near as I can tell, is the only IDE that does allow this). Since I do as much work outside of an IDE as in one, I lump everything in one root directory so that I'm not constantly fiddeling with my CLASSPATH.


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
As your projects start to get more complex, you're really going to want to keep your source files seperate from your generated files, including your compiled class files.
At a very minimum I'd create a classes directory to keep your compiled class files seperated from your source.
You'd have something like

This is the "standard" way of doing things. As your builds get more complicated there are more things you will need to do, like build for testing, jaring, packaging for deployment, etc, and having a well-organized, standard hierarchy will make your life a lot easier.
Also, to set your classpath you can then just add your "classes" directory to your classpath, along with any other lib directory or jar files your project is using.
[ May 28, 2003: Message edited by: Rob Ross ]
[ May 28, 2003: Message edited by: Rob Ross ]

Rob
SCJP 1.4
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I don't recall where I got this, but something made it sound like a common convention, so I re-orged my stuff like this:
src - Java source
classes - classes generated by Ant compiles
distribution - jars & such that I generate
docs - JavaDoc that I generate
lib - other people's jars & such
ant - Ant scripts
I do most of my compile & test right in the Forte IDE which does not use all these directories. I build for execution outside the IDE with Ant. The Ant scripts put things in the classes, docs and distribution directories.
Anybody else?


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How should I organize classes and source on disk?