Incorporating a class from another directory

P

poener

Hello all,

I think this should be a pretty simple question, but I can't figure out
how to do it...

I have written a class in $WHATEVER/Scripts, and I want to use it in a
program that I am writting in $WHATEVER/Control. Since it looks like I
can't use a simple include filename like I would do in C, I would like
to know how should I do it.

=================

A more complicated issue...

The class in $WHATEVER/Scripts reads which devices are connected to the
USB ports of the computer and generates a series of objects with allow
to communicate with those devices.

Now I have to include this class on each java program which wants to
communicate with any of those devices, but I think a much better way of
doing would be to run this communications class once, independently,
and then, when a program needed to communicate with one or several of
these devices, that it could talk to this communications class and
access its objects to talk to the devices... Is there a way of doing
this?

Thank you,

- J.
 
A

Andrew Thompson

poener wrote:
....
I have written a class in $WHATEVER/Scripts, and I want to use it in a
program that I am writting in $WHATEVER/Control. Since it looks like I
can't use a simple include filename like I would do in C, I would like
to know how should I do it.

Include the directories containing all necessary classes
on the classpath at runtime.
Now I have to include this class on each java program which wants to
communicate with any of those devices,

It needs to be on the *classpath* of each Java program
that uses it, but there only needs to be one copy of it.
...but I think a much better way of
doing would be to run this communications class once, independently,
and then, when a program needed to communicate with one or several of
these devices, that it could talk to this communications class and
access its objects to talk to the devices... Is there a way of doing
this?

The Singleton design pattern would seem to fit this situation.

Andrew T.
 
A

Andrea Desole

poener said:
Now I have to include this class on each java program which wants to
communicate with any of those devices, but I think a much better way of
doing would be to run this communications class once, independently,
and then, when a program needed to communicate with one or several of
these devices, that it could talk to this communications class and
access its objects to talk to the devices... Is there a way of doing
this?

are you talking about communication between two virtual machines? Yes,
you can do it, but you have to use something like RMI, or socket
communication, or something like that. I don't think there is a way to
facilitate the communication between two instances of the JVM running on
the same machine.
You really don't want to do it. If you can use one instance of your
class per virtual machine it's much better.
 
P

poener

Ok, thanks for your help, I will use javapath and avoid communication
between virtual machines, since it seems to be too complicated.

- J.
 
P

poener

Andrew said:
poener wrote:
...

Include the directories containing all necessary classes
on the classpath at runtime.

But "javac -classpath ../Scripts *.java" will override all other
classpaths that I have defined in my system...
 
A

Andrew Thompson

poener said:
But "javac -classpath ../Scripts *.java" will override all other
classpaths that I have defined in my system...

Which is why you don't type that, but instead consult
the docs. re the separator that allows you to build up classpaths
from more than one location. (...like - duhhh.)

Andrew T.
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top