0

I want to run a JAR file from my own main(). Let's say I have an A class which has a main() method. The main() method of A will run a new main() method in another file B.jar. This is how I run the B.jar file from main() method of A.jar: here.

The problem is that I do not have access to the B.jar Java source code. What I need to know is how many threads are running in the B.jar main() method.

There was another discussion here about getting the list of all Threads running in a Java application here.

Now what I need is a list of the threads which are spawned by B.jar. Or at least an array of the Thread IDs.

Community
  • 1
  • 1
Reza
  • 2,058
  • 3
  • 20
  • 33
  • The solution in the question you linked to will create a new process - is that what you are looking for? Or do you want to run the code from B.Jar in thread(s) from your process? – RonK Oct 14 '12 at 14:38
  • How are you running the app? Is this jar as a library in your code, did you find the main class/or starting point method in the other jar? – PbxMan Oct 14 '12 at 14:53
  • I think the question was not clear enough. I have edited it so that my question will become clearer. – Reza Oct 14 '12 at 16:31

3 Answers3

0

The method Thread.activeCount() will give you the number of threads in the current ThreadGroup. If the threads in the external jar don't define new thread groups, then the runtime by default will put this threads in the same group as the calling thread (i.e. your thread). You can call this method before and after executing the jar and see how much it changes.

Update - If you also want to know which threads were spawned by the new jar, use Thread.enumerate, also before and after, filter for threads that are in both arrays. The remaining threads should be the ones created by the classes in the jar file.

vainolo
  • 6,907
  • 4
  • 24
  • 47
  • Thank you, it was a good suggestion but it is not exactly what I need. I have edited my question, maybe it is now clearer. – Reza Oct 14 '12 at 16:41
0

Keep hold of your root/parent thread of JAR B processes. At any time, use this root/parent.activeCount() to get your B created threads count.

Yogendra Singh
  • 33,927
  • 6
  • 63
  • 73
  • The problem is that I do not have access to the B.jar source code. I have edited my question, maybe it is now clearer. – Reza Oct 14 '12 at 16:33
0

If you are running B.jar in a separate process as per the citation you linked to, its threads are in that process, not this one, and you have no access to them whatsoever. If you need access to the threads for some (strange) reason, you will have to (find and) run B.jar's main() method directly in the current JVM.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • If by finding you mean accessing the source code, I should say it not possible (Let me make it straight! B.jar is [`SPECjvm 2008.jar`](http://www.spec.org/jvm2008))! So I don't have access to the source code. Is there any other way to run the second JAR file in the same JVM? If it helps, I need to have the threads in B.jar because I need to check the track of Thread migrations which are happening in it. – Reza Oct 15 '12 at 03:32