For real Java code, this is possible using the JavaCompiler interface. However, it's very inconvenient to use since it's just an interface to a real Java compiler that expects to compile entire class definitions found in files.
The easiest way to execute code supplied at runtime would be to use the Rhino JavaScript engine.
Both of these options have been only in Java 6, though I believe the scripting interface existed before, so you could use Rhino in an earlier JRE if you download and add it to the classpath.
Answer from Michael Borgwardt on Stack OverflowFor real Java code, this is possible using the JavaCompiler interface. However, it's very inconvenient to use since it's just an interface to a real Java compiler that expects to compile entire class definitions found in files.
The easiest way to execute code supplied at runtime would be to use the Rhino JavaScript engine.
Both of these options have been only in Java 6, though I believe the scripting interface existed before, so you could use Rhino in an earlier JRE if you download and add it to the classpath.
Javassist
You would need to use a bytecode manipulation library such as Javassist (Wikipedia), in order to run an arbitrary string that is provided at runtime. Javassist allows you to create a CtClass based on a string representing source code; and can then turn this into compiled Class object via a particular classloader, so that the class is then available to your application. Other libraries would need to do something similar to these two steps in order to achieve the same thing.
So it is possible, but it's very heavyweight and is likely to make your application very hard to reason about. If at all possible, consider designing a very flexible class statically, and having it accept parameters that control its behaviour.
dynamic - How could I call Java code dynamically? - Stack Overflow
[Java] Dynamic Code Execution
sockets - "Dynamically" executing code in Java - Stack Overflow
difference between dynamic and static type checking
Look into BeanShell or Groovy. Both will give you reasonable solutions--but those solutions rely on my interpretation of your problem, which may be flawed.
I've used the JavaScript engine shipped with Java 6 and it works quite well. The performance of the engine is very very decent.
Check the page http://java.sun.com/javase/6/docs/technotes/guides/scripting/programmer_guide/index.html
You can do it in a lot of ways. As people said, you can implement using a lot of desing patterns like Builder.
But here is a simple way to implement your client:
public class DateClient {
public static void main(String[] args) throws IOException {
String serverAddress = "ipAdress";
Socket s = new Socket(serverAddress, 9090); // use the right port
BufferedReader input
= new BufferedReader(new InputStreamReader(s.getInputStream()));
char answer = input.readLine().charAt(0);
switch (answer) {
case 'a':
//do something for a
break;
case 'b':
//do something for b
break;
case 'c':
//do something for c
break;
default:
System.out.println("No valid entry");
}
System.exit(0);
}
}
It needs verification while reading the package from server, or it may throw exceptions.
Its not really "dynamically". You mean to execute a command on a given request from the socket?
Rule #1 of building services: Sanitize all input.
For this reason I would avoid any kind of methodology that uses Reflection to obtain method names and execute them that way.
Essentially you are looking for a bit of logic to determine what to do with the input request.
This could be done a number of ways.
- Using a switch case on a String name.
- Using a HashMap to map the String name to a generic interface (void run( Object input ))
- Using an event dispatcher mechanism to alert listeners of input request.
How you decide to implement this is up to you and really depends on your goals.
What you determine to be a "message" from the socket involves formatting. I am assuming that you are formatting the data so the receiver knows a message of X bytes is coming so it can be buffered and processed only when the entire message has been received.
Formatting of data on the socket is beyond the scope of this question. There are lot of resources for designing and building RPC services. May google be with you.