Dynamically loading a class at the Client side program

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);

            ClassLoader classLoader = ClassLoader.getSystemClassLoader();
            Class cls = classLoader.loadClass(classF);
            Object obj = cls.newInstance();

        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException ex) {
        //catch (MalformedURLException ex) {
        return true;
        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.”

Leave a Reply

2 Comment threads
0 Thread replies
Most reacted comment
Hottest comment thread
1 Comment authors
Jason Recent comment authors
newest oldest most voted
Notify of

You should load compiled class, not the source code. Try to compile the class before uploading it to the server, and then send the produced by compiler *.class file instead of *.java file.
You can compile the class using command line compiler:
javac MyClass.java – it will produce the compiled MyClass.class file in the same directory (or use compile/build command from your IDE and look for the compiled class in the output directory of the IDE – I am using IntelliJ/Eclipse so I am not sure which one it will be in the NetBeans).


It seems Netbeans will compile code even with errors, and then throw the RuntimeException at the runtime, in this case ComputePi class might have an error. So need to check the class source code. You may paste the source code here to check.