"Meidan" <meidan.alon@gmail.com> wrote in news:1134587714.717782.182840
@g14g2000cwa.googlegroups.com:
>> Well, you'll need to make sure Foo is in the correct place in your
>> classpath when compiling
>
> But I didn't compile Foo, I only have the .class.
Well, you'll need to make sure Foo is in the correct place in your
classpath when compiling Bar.
ie: javac -classpath path/to/Foo/ Bar.java

Signature
Beware the False Authority Syndrome
> But I didn't compile Foo, I only have the .class.
That's not a problem, all you need is the .class. But, it needs to be
in your classpath, so that the compiler can find it; the compiler needs
to see it so that it can check your code (e.g. to see whether or not
Foo contains a main() method, or else it won't be able to generate code
for the Foo.main() call).
Alternatively, use the reflection approach mentioned earlier:
try {
Class c = Class.forName("Foo");
String[] args = new String[] { "Hello", "world" };
Method m = c.getMethod("main", new Class[] { args.getClass() });
m.invoke(null, args);
} catch (Exception e) {
// Possible exceptions include ClassNotFoundException,
// NoSuchMethodException, InvocationTargetException
e.printStackTrace();
}
Meidan - 14 Dec 2005 20:07 GMT
> > But I didn't compile Foo, I only have the .class.
>
[quoted text clipped - 16 lines]
> e.printStackTrace();
> }
I get a java.lang.ClassNotFoundException when Class.forName("Foo") is
executed.
I'm using Eclipse and I found in project properties the parameter "java
build path", I added to it the location of my .class file. But still no
luck.......
Gordon Beaton - 14 Dec 2005 20:40 GMT
> I get a java.lang.ClassNotFoundException when Class.forName("Foo")
> is executed.
So you've got classpath issues. Does "Foo" have a real name? Does it
belong to a package?
If so, the classpath should not point to the directory containing the
classfile. It should point to a directory containing a hierarchy of
directories that correspond to the component parts of the package name.
Learn more about the necessary relationship between the classpath and
the directory structure here:
http://www.yoda.arachsys.com/java/packages.html
Type "javap Foo" to see what the file contains if you don't already
know.
/gordon

Signature
[ do not email me copies of your followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
Meidan - 15 Dec 2005 07:31 GMT
> > I get a java.lang.ClassNotFoundException when Class.forName("Foo")
> > is executed.
[quoted text clipped - 19 lines]
> [ do not email me copies of your followups ]
> g o r d o n + n e w s @ b a l d e r 1 3 . s e
My file system structure is:
Exercise
FruitGamePlayer
.metadata(dir)
Player
.metadata
.classpath
.project
Tester.java
MyPlayer.java
(and some more .class files for internal use in MyPlayer)
FruitGamesServer
.metadata(dir)
GamesServer
.project
GameDefinition.class
GameServerMain.class
HandlingServers.class
PlayerServer.class
.classpath
.project
What do I have to do in order to call GameServerMain.main() from
Tester.java?
Note that I can call it from the command line by java -classpath .;..
GameServerMain from within the GameServer Directory.
Meidan - 15 Dec 2005 08:19 GMT
The contents of my main .classpath file:
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="FruitGamePlayer/Player"/>
<classpathentry kind="src" path="FruitGameServer/GameServer"/>
<classpathentry kind="con"
path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="con" path="FruitGameServer/GameServer"/>
<classpathentry kind="con" path="FruitGameServer/GameServer"/>
<classpathentry kind="output" path="FruitGamePlayer/Player"/>
<classpathentry kind="output" path="FruitGameServer/GameServer"/>
</classpath>