Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8290989
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T12:58:42+00:00 2026-06-08T12:58:42+00:00

I have some code that dynamically loads classes from several JARs in a directory.

  • 0

I have some code that dynamically loads classes from several JARs in a directory. Because the code in the JARs is untrusted, I wish to protect the main application from poorly-coded or malicious dynamic classes by “sandboxing” them. One thing I’m trying to do is “timebox” the untrusted code’s execution, and kill it if it’s taking too long (because it’s stuck in an infinite loop, for instance). I’m trying to implement this using Callable and Future, not because I want to run the tasks in parallel (I don’t), but to take advantage of work done to provide a return value, capture exceptions, and cancel running tasks. However, when I run it on some test code, I’m finding that invoking Future.cancel(true) doesn’t seem to cancel the task.

I’ve seen several questions (such as this one) on StackOverflow about why this would occur, and the answer is always the same: cancel(true) works by invoking interrupt() on the thread running the Callable, which requires that the cancelled task block on an interrupt()ible operation (such as Thread.sleep()) before the cancellation will take effect. Since I don’t control the code being executed, there’s no guarantee that it will ever do that, so I can’t be sure that I’ve cancelled it.

Is there any way that I can run dynamically-loaded code, then later kill it in progress and be sure it dies, even if it never blocks on an interrupt()ible operation?

// Assume: ExecutorService pool, Callable<T> task,
//  long timeout, TimeUnit timeUnit
Future<T> future = pool.submit(task);
T result;

try {
    result = future.get(timeout, timeUnit);
    // do something with result
} catch (TimeoutException ex) {
    future.cancel(true);
    // handle timeout
} catch (ExecutionException ex) {
    // handle exeception thrown by task
} catch (InterruptedException ex) {
    // handle InterruptedException
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T12:58:44+00:00Added an answer on June 8, 2026 at 12:58 pm

    Next to Thread.stop and Thread.destroy, you can for ultimate sandboxing, spawn a whole JVM in a subprocess with Process p = Runtime.getRuntime().exec( cmd );. The process can really be destroyed with Process.destroy if necessary.

    To ease the process creation, use ProcessBuilder. Also, look a available tutorial (this one is in french) to execute process, wait until completion (with waitFor), and manage streams.

    I’ve seen it working in a product where we needed to convert ps document to pdf. We were dumping the ps file, executing ps2pdf, then reading the output file.

    Pro/cons of Process:

    • (+) real sandboxing into a separate process
    • (-) communication between
      JVM harder to deal with
    • (-) complicates the deployment of the app
      (path of the JVM, options, current directory, etc.)

    Note that Thread.stop and Thread.destroy (I’m not sure it’s been actually implemented) are discouraged because they are unsafe with respect to locks acquisition and release. Make sure you read Why Are Thread.stop, Thread.suspend,
    Thread.resume and Runtime.runFinalizersOnExit Deprecated?
    If the untrusted module running in a separate thread does not mess with locks of the application, only at worst with its own locks, then Thread.stop might still be the best option.

    Pro/cons of Thread.stop

    • (+) easy to use
    • (+) communication between trusted and untrusted code is easy
    • (-) deprecated
    • (-) possibly unsafe in some very specific situation that must be assessed first
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some javascript code that loads images dynamically in a web page: imageSrc
I have some code that dynamically creates a new button through JavaScript that when
I have a trivia page that is dynamically generated by some javascript code. It
I have some Javascript code that needs to be dynamically created as a String
I have some code that will change the background color of a specific label
I have some code that is supposed to return an NSString. Instead it is
I have some code that generates Visio masters for me, and some masters have
I have some code that causes the box2d physics simulation to stutter forever after
I have some code that runs a model in a loop. Each iteration of
I have some code that shows results in a table Right now it will

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.