Hi again,
It's odd, but I often get an error. Here's the Server program.. but I doubt you need to read it all. Please just scroll down to "// The line below is what give me problems:
".
package remoterun;
import java.net.*;
import java.io.*;
import java.lang.reflect.*;
* <p>Copyright: Copyright ms2000 (c) 2005</p>
public class Server2 {
public static void main(String args[]) throws Exception {
int numParameters;
int port = 6789;
boolean isThere = false;
String className, methodName;
Object[] parameters;
ServerSocket welcomeSocket = new ServerSocket(port);
for (; ; ) {
/**
* Create a new socket, called connectionSocket, when some client knocks
* on welcomeSocket. This socket has the same port number. TCP then
* establishes a direct virtual pipe between the client socket and
* connectionSocket at the server so the client and server can send bytes
* to each other over it.
*/
Socket connectionSocket = welcomeSocket.accept();
// Get number of parameters
DataInputStream in = new DataInputStream(new BufferedInputStream(
connectionSocket.getInputStream()));
numParameters = in.readInt();
// Get the parameters for the method to be invoked.
parameters = new Object[numParameters];
ObjectInputStream objStream = new ObjectInputStream(
connectionSocket.getInputStream());
for (int i = 0; i < numParameters; i++) {
parameters[i] = objStream.readObject();
}
// read the class
File program = (File) objStream.readObject();
//Class2.test();
//program.deleteOnExit();
//connectionSocket.close();
//welcomeSocket.close();
System.err.println(program); // It prints the program name correctly, e.g. Class2.java
// Receiving some String parameters...
BufferedReader inFromClient = new BufferedReader(new
InputStreamReader(connectionSocket.getInputStream()));
className = inFromClient.readLine();
methodName = inFromClient.readLine();
// The line below is what give me problems:
Class classDefinition = Class.forName("remoterun." + className + ".java");
Object object = classDefinition.newInstance();
Method[] theMethods = classDefinition.getMethods();
for (int i = 0; (i < theMethods.length) && (!isThere); i++) {
if (theMethods[i].getName().equals(methodName)) {
isThere = true;
theMethods[i].invoke(object, parameters);
}
}
}
}
}
The error I get is:
java.lang.ClassNotFoundException: remoterun.Class2.java
at java.net.URLClassLoader$1.run(URLClassLoader.java:199)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:187)
at java.lang.ClassLoader.loadClass(ClassLoader.java:289)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:274)
at java.lang.ClassLoader.loadClass(ClassLoader.java:235)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:302)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:141)
at remoterun.Server2.main(Server2.java:94)
Exception in thread "main"
(I also get a dialog box saying sthg like, "A fatal exception occured.. will exit now".)
It's really odd, becuase there were rare times when it works although I don't change the program...
I tried changing the problem line to stuff like:
Class classDefinition = Class.forNameclassName + ".java");
or
Class classDefinition = Class.forName(className);
but with no use. Same error. Can someone please pinpoint the problem?
I'm sure the program does get to the Server, because it can print out the file name.
I'd appreciate some assistance in this.
Thank you.