It seems a crazy idea but I wanna know the feasibility of the idea.
I used to run a command in a loop in zsh script:
timeout 10s java -cp $Lib $app $args 2>error.txt
which contains several points:
- Run the Java program in 10 seconds, if exceeding the time limit, return error code
124; - Load and unload classpaths (
$Lib) dynamically since each loop contains a different$Lib; - Mutable arguments --
$args; - Redirect standard error stream (
java.lang.Throwablein Java) to a text file
I've tried to translate into Java code snippets since I would not like to use zsh+Java anymore. Instead, I'd like to simply implement my tool in pure Java code. Let's browse the four points above:
- I don't know how to start/run a method in a new thread (or process) with a specified time limit;
- There is already an available solution for loading and unloading classpath: link1, link2;
- Since I even don't know I should use a new process or a new thread, the solution to pass different arguments is unknown;
- So is it possible to catch an exception from a method in a different thread/process
Maybe there is no solution to my idea, and I am curious about the reason and limitation of the Java thread/process model.