0

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:

  1. Run the Java program in 10 seconds, if exceeding the time limit, return error code 124;
  2. Load and unload classpaths ($Lib) dynamically since each loop contains a different $Lib;
  3. Mutable arguments -- $args;
  4. Redirect standard error stream (java.lang.Throwable in 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:

  1. I don't know how to start/run a method in a new thread (or process) with a specified time limit;
  2. There is already an available solution for loading and unloading classpath: link1, link2;
  3. Since I even don't know I should use a new process or a new thread, the solution to pass different arguments is unknown;
  4. 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.

  • I think you could. You would need a loop that iterate over a tuple of lib, app, args and executing java on those values. I can't help you because it's been over 20 years since I wrote a shell script. – Cheng Thao Dec 18 '21 at 06:36

0 Answers0