I'm working on a client-server
application as part of my course assignment. The program is basically just calculating prime numbers, gcd, etc.
The client side is a GUI and the server side handles requests and executes calculations.
There is a folder called Contract
that holds the compute-tasks classes for the calculations of prime numbers, gcd, and pi.
The user clicks on a "Calculate" button, sends a request to the server to check if the corresponding Compute class is available in the folder. If it is found, then the server does the calculations by using the parent interface "Task" and sends the results back to the client. If the file isn't there, then the server sends an error message to the client side.
But here is my problem:
I've managed to check if the file exists with the help of my previous SO question. Now, if I don't have the file in the folder before executing the server and client programs, when I try to dynamically load the class at the server-side, I get this exception at the client-side. Furthermore, if the file is not in the folder, the user has to upload the file in the folder and then if the server sees it's there, it has to load the class file at runtime.
How do I resolve this issue?
Server Side Code to Check if file exists and load class:
public boolean checkIfFileExists(String classFile){
String path = "./src/Server/Contract/"+classFile+".java";
String classF = "Server.Contract."+classFile;
File file = new File(path);
if(file.exists()){
try{
ClassLoader classLoader = ClassLoader.getSystemClassLoader();
Class cls = classLoader.loadClass(classF);
Object obj = cls.newInstance();
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException ex) {
}
//catch (MalformedURLException ex) {
//}
return true;
}
else{
return false;
}
}
Basically what I'm expecting this method to do is return true if the file exists and load the class and then the server executes the remaining part of my code and return false if it doesn't exist.
P.S. the server and client are running on the same machine with the server being the localhost.
How do I let the client-side use the class after the server has loaded it? Also, I'm using Netbeans and JDK 1.8 I this helps. These are what my course uses.
Assignment Requirement: " However, when there is an exception occurred (e.g. a compute-client wants the compute-server to perform a compute-task, but forgets uploading the Java class of the compute-task) onto the class repository of the compute-server, the compute-server will create a CSMessage object and sends it back to the compute-client. Note: the CSMessage follows the interaction contract by implementing the Task interface. By calling the getResult() method, the computeclient will know the problem and fix it later on."