2

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."

Shane D'Silva
  • 405
  • 1
  • 4
  • 11
  • Whenever I try to create a new Class in Netbeans, it saves it as a .java file. Is this okay? – Shane D'Silva Aug 02 '19 at 05:10
  • @OP If you want to add more to your question, just edit the original and add it. It will get missed in the comments. – flakes Aug 02 '19 at 05:14
  • Why is the class not with the server application in the first place? – Lalit Mehra Aug 02 '19 at 05:30
  • @flakes I'll do that next time – Shane D'Silva Aug 02 '19 at 06:01
  • @lalitmehra I'm not sure. Netbeans creates the .class files under the 'Build' folder for some reason. If I rename my .java files to .class files I get import errors at the client side code – Shane D'Silva Aug 02 '19 at 06:03
  • If you have access to both the client and server build both of them. That should create the .class files after compilation. You should not rename .java files as .class files. .java files are source code files while .class are the binary equivalents understood by the JVM. – Lalit Mehra Aug 02 '19 at 06:17
  • @LalitMehra I've posted the assignment requirement as to why I need to do this – Shane D'Silva Aug 02 '19 at 15:02
  • @LalitMehra So once the client knows what the problem is, he will paste the file back into the folder and the program has to continue as is. However, I've done a workaround. Supposedly I build the project with all files present, and then delete a file, the server throws the error, and when the client puts back the file, the program goes ahead. This works because I already compiled all the files before executing the programs. But I'm not sure if this is what my course wants. Think they want it to be dynamic. – Shane D'Silva Aug 02 '19 at 15:07
  • @ShaneD'Silva check out the link shared by Andrzej Jaromin – Lalit Mehra Aug 03 '19 at 11:35

2 Answers2

0

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.

Mahen
  • 1
  • I think you mean my client side program. Yes I need it to behave that way. In my assignment it is said that if the user has started the program without ensuring all the classes are available the server should throw an error that the client side should display – Shane D'Silva Aug 02 '19 at 06:10
  • In this case ComputePi class is present but the class is having compilation error, it has to be compiled properly to load without any error. either compile the class file or paste the file content here to check the error. – Mahen Aug 02 '19 at 11:17
0

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

Andrzej Jaromin
  • 269
  • 2
  • 6
  • Is there a way to compile and load the class through code instead of using the cmd? Can't exactly tell my professor to compile the class when he's marking my assignment – Shane D'Silva Aug 02 '19 at 06:06
  • even if there is a way to compile and load the classes through code, why do you want to do that. Just compile the source code files properly and check if the .class file for the same is available before running your client application – Lalit Mehra Aug 02 '19 at 06:18
  • If you really want to compile the code - check it out: https://stackoverflow.com/questions/21544446/how-do-you-dynamically-compile-and-load-external-java-classes – Andrzej Jaromin Aug 02 '19 at 06:36
  • @AndrzejJaromin I've posted the assignment requirement as to why I need to do this – Shane D'Silva Aug 02 '19 at 15:03