Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How should I organize classes and source on disk?

 
Timothy Chen Allen
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic