import, classpath, and directories

C

cppaddict

When I organize new Java projects I like to put
all the files in a new folder. To avoid class
not found errors at runtime, I've been putting
the new folder in my CLASSPATH. This method is
obviously a hack, and it results in an
ever-growing CLASSPATH.

What can I do to avoid this?

My goals are:

1. Be able to put new projects in new directories
(prefereably anywhere on my computer, but I'll
settle for below my CLASSPATH).

2. Be able to reference reusable library packages
that reside in my CLASSPATH using import
statements.

As a related aside, how does java find things in
the java package even though the path to it isn't
in my CLASSPATH? Is the path to these files
somehow built-in during installation?

Thanks for any advice,
cpp
 
R

Roedy Green

As a related aside, how does java find things in
the java package even though the path to it isn't
in my CLASSPATH? Is the path to these files
somehow built-in during installation?

In the old days you had to help it. Now Java.exe looks in the registry
to find out where the current java is installed. From there it can
find all its own stuff.
 
C

cppaddict

gak. use an IDE not idea. Darn spell checker.

Roedy,

I would prefer to do it by hand, rather than use an IDE. I do all my
text editing in VIM and want to keep it that way. Any suggestions
that don't require an IDE?

Thank for your reply,
cpp
 
R

Roedy Green

I would prefer to do it by hand, rather than use an IDE. I do all my
text editing in VIM and want to keep it that way. Any suggestions
that don't require an IDE?

Then you need something that does that function of an IDE. SlickEdit
will do that for example, which is not a full blown IDE.

You need something that will poke a different classpath into the
registry on command.

You might experiment with import/exporting the classpath with regedit.

see http://mindprod.com/jgloss/registry.html
 
W

Will Hartung

cppaddict said:
Roedy,

I would prefer to do it by hand, rather than use an IDE. I do all my
text editing in VIM and want to keep it that way. Any suggestions
that don't require an IDE?

Create a env script for each project that sets the CLASSPATH appropriately.
At the start of your session, run that and go on you merry way.

Also, you can consult the Java documentation regarding place JARs within the
JRE/JDK install area so that thoes are implicitly put on all of your
CLASSPATHs.

I have a common directory that my script uses so that whenever I place a JAR
in that directory, the script places it on my CLASSPATH.

Any arbitary spot on your disk will require an entry in your CLASSPATH,
that's simply the way of the world.

You can also do something like this:
/home
/cppaddict
/java
/cppaddict
/project1
TestClass.java <-- package cppaddict.project1
/project2
TestClass.java <-- package cppaddict.project2
/util
UtilClass.java <-- package cppaddict.util

Then each project has his own directory, and you have a shared directory for
utils. Here you'd simply put /home/cppaddict/java on your CLASSPATH and you
have access to everything you write.

Since you're using VIM, I assume you're running UNIX, so if you want to you
can use symbolic links to maintain the directory and package structure but
put your code in different places.

Regards,

Will Hartung
([email protected])
 
R

Roedy Green

Create a env script for each project that sets the CLASSPATH appropriately.
At the start of your session, run that and go on you merry way.

In windows, if you change the classpath in the DOS box, it does not
affect what you do in Windows, just in that dos box until you close
it.

The only official way to change the classpath for all apps is poking
around in the control panel which is clumsy as all get out.

Surely someone has written utilities for doing universal SET changes
from bat files.
 
S

Sam

cppaddict said:
When I organize new Java projects I like to put
all the files in a new folder. To avoid class
not found errors at runtime, I've been putting
the new folder in my CLASSPATH. This method is
obviously a hack, and it results in an
ever-growing CLASSPATH.

What can I do to avoid this?

My goals are:

1. Be able to put new projects in new directories
(prefereably anywhere on my computer, but I'll
settle for below my CLASSPATH).

2. Be able to reference reusable library packages
that reside in my CLASSPATH using import
statements.

As a related aside, how does java find things in
the java package even though the path to it isn't
in my CLASSPATH? Is the path to these files
somehow built-in during installation?

Thanks for any advice,
cpp

You can use the "cp" or "classpath" parameter to the java command, so
you can have different classpaths for different projects.

When you run javac and java in windows, they are ".exe" files so they
may automatically have some different method of accessing the java
class libraries, although I don't know what it is, nor exactly where
they are kept.

Note that some servers and ides use thier own classpaths as well.

Regards,
Sam90
 
H

Hemal Pandya

cppaddict said:
When I organize new Java projects I like to put
all the files in a new folder. To avoid class
not found errors at runtime, I've been putting
the new folder in my CLASSPATH. This method is
obviously a hack, and it results in an
ever-growing CLASSPATH.

What can I do to avoid this?

My goals are:

1. Be able to put new projects in new directories
(prefereably anywhere on my computer, but I'll
settle for below my CLASSPATH).

2. Be able to reference reusable library packages
that reside in my CLASSPATH using import
statements.

Packages are your friends. Put each project in a separate package. To
take a simple example:

- C:/javaprojects is in classpath
- Project1 classes are in package project1 and the directory
c:/javaprojects/project1
- Project2 classes are in package project1 and the directory
c:/javaprojects/project2
- In project2.SomeClass you can import project1.SomeClass

Does this work?
 
S

Stefan Poehn

cppaddict said:
Roedy,

I would prefer to do it by hand, rather than use an IDE. I do all my
text editing in VIM and want to keep it that way. Any suggestions
that don't require an IDE?

Let ant take care about the classpath in your project.
(http://ant.apache.org/)
 
R

Roedy Green

if you create a little bat file to do your compile, you can just put
the classpath in there, either with set on the javac.exe or java.exe
command line.

However, if you have . on the classpath, all you need in to have the
CWD set to the project directory and you are set.
 

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

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,170
Latest member
Andrew1609
Top