0

So I am working on a multithreaded client server program in java, and occasionally the server side of the program needs to start a new Thread of a class which I have made that implements Runnable. The problem is that the Runnable class uses classes from a jar file that I have in its same directory.

When I run the Thread program on my own and I include the -classpath flag and specify the jar's classpath, it works fine. However, when I start the Thread from inside the server program, it does not work properly (presumably because it cannot find the jar). So my question is, is there a way to start the Thread of the Runnable from inside the server program with some reference to the jar so that it can use the classes that it needs from the jar?

(The jar is some sqlite-jdbc thing so I can access a database from inside my code)

Thanks in advance for any help.

  • 1
    You can do it, but you'll need to dynamically load the jar at runtime using a classloader. Is there a certain reason why running it with the -classpath flag isn't a solution? – blacktide Apr 01 '16 at 23:22
  • This is my first time trying to make any sort of multithreaded program, and it's my first time using java Threads inside of code so I might just be misunderstanding or overlooking something, but the way I understand it is that because the class that needs the jar is being called using "Thread thread = new Thread(Class Constructor); Thread.run()" from inside of another program, there was no way to add flags to the startup since I'm not actually doing the startup in a cmd window. – Arobbins530 Apr 02 '16 at 11:42
  • All new threads still live within the same process, so starting the original process with the jar on the classpath is the right way to do it. If you're using an IDE, you can just add the jar to the build path for your project. Here's how to do it in [IntelliJ](http://stackoverflow.com/a/1051705/2446208) and [Eclipse](http://stackoverflow.com/a/3280384/2446208). – blacktide Apr 02 '16 at 14:12
  • ^This worked! Thank you so much. – Arobbins530 Apr 02 '16 at 22:44

0 Answers0