Skip to content

Commit 36037a4

Browse files
author
lifey
committed
move to java 21 tools.jar does not exist anymore
1 parent 60adbaf commit 36037a4

3 files changed

Lines changed: 32 additions & 161 deletions

File tree

pom.xml

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,16 @@
1515
<plugin>
1616
<groupId>org.apache.maven.plugins</groupId>
1717
<artifactId>maven-compiler-plugin</artifactId>
18-
<version>2.3.2</version>
18+
<version>3.13.0</version>
1919
<configuration>
20-
<source>1.6</source>
21-
<target>1.6</target>
20+
<source>21</source>
21+
<target>21</target>
2222
</configuration>
2323
</plugin>
2424
<plugin>
2525
<groupId>org.apache.maven.plugins</groupId>
2626
<artifactId>maven-jar-plugin</artifactId>
27-
<version>2.4</version>
27+
<version>3.4.2</version>
2828
<configuration>
2929
<archive>
3030
<addMavenDescriptor>false</addMavenDescriptor>
@@ -69,7 +69,7 @@
6969
<plugin>
7070
<groupId>org.apache.maven.plugins</groupId>
7171
<artifactId>maven-assembly-plugin</artifactId>
72-
<version>2.4</version>
72+
<version>3.7.1</version>
7373
<configuration>
7474
<descriptors>
7575
<descriptor>assembly/bin.xml</descriptor>
@@ -114,34 +114,13 @@
114114
<dependency>
115115
<groupId>junit</groupId>
116116
<artifactId>junit</artifactId>
117-
<version>4.11</version>
117+
<version>4.13.2</version>
118118
<scope>test</scope>
119119
</dependency>
120120
<dependency>
121121
<groupId>com.lexicalscope.jewelcli</groupId>
122122
<artifactId>jewelcli</artifactId>
123123
<version>0.8.9</version>
124124
</dependency>
125-
<!-- mvn install:install-file -Dfile=tools.jar -DgroupId=com.oracle -DartifactId=tools -Dversion=7.17 -Dpackaging=jar-->
126-
<!--dependency>
127-
<groupId>com.oracle</groupId>
128-
<artifactId>tools</artifactId>
129-
<version>7.45</version>
130-
131-
</dependency-->
132-
<dependency>
133-
<groupId>com.sun</groupId>
134-
<artifactId>tools</artifactId>
135-
<version>7.0</version>
136-
<scope>system</scope>
137-
<systemPath>${java.home}/../lib/tools.jar</systemPath>
138-
</dependency>
139-
<dependency>
140-
<groupId>org.javassist</groupId>
141-
<artifactId>javassist</artifactId>
142-
<version>3.28.0-GA</version>
143-
</dependency>
144-
145125
</dependencies>
146-
147126
</project>

src/main/java/com/performizeit/jmxsupport/JMXConnection.java

Lines changed: 12 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,9 @@
1616
*/
1717
package com.performizeit.jmxsupport;
1818

19-
import com.sun.tools.attach.AgentInitializationException;
20-
import com.sun.tools.attach.AgentLoadException;
21-
import com.sun.tools.attach.AttachNotSupportedException;
22-
import java.io.File;
19+
2320
import java.io.IOException;
24-
import java.lang.reflect.Method;
2521
import java.net.MalformedURLException;
26-
import java.net.URL;
27-
import java.net.URLClassLoader;
2822
import java.util.HashMap;
2923
import java.util.Map;
3024
import java.util.logging.Level;
@@ -48,9 +42,7 @@ public class JMXConnection {
4842
String userName = "";
4943
String userPassword = "";
5044
JMXServiceURL serviceURL;
51-
private static final String CONNECTOR_ADDRESS =
52-
"com.sun.management.jmxremote.localConnectorAddress";
53-
private String connectURL;
45+
private final String connectURL;
5446
private boolean originalThreadContentionEnabledValue;
5547

5648
public boolean isOriginalThreadContentionEnabledValue() {
@@ -61,38 +53,7 @@ public void setOriginalThreadContentionEnabledValue(boolean originalThreadConten
6153
this.originalThreadContentionEnabledValue = originalThreadContentionEnabledValue;
6254
}
6355

64-
public JMXConnection(String pid) throws AttachNotSupportedException, IOException, AgentLoadException, AgentInitializationException {
65-
addToolsJar();
66-
connectURL = pid;
67-
// attach to the target application
68-
com.sun.tools.attach.VirtualMachine vm =
69-
com.sun.tools.attach.VirtualMachine.attach(pid.toString());
70-
JMXServiceURL u;
71-
try {
72-
// get the connector address
73-
String connectorAddress =
74-
vm.getAgentProperties().getProperty(CONNECTOR_ADDRESS);
75-
76-
// no connector address, so we start the JMX agent
77-
if (connectorAddress == null) {
78-
String agent = vm.getSystemProperties().getProperty("java.home")
79-
+ File.separator + "lib" + File.separator
80-
+ "management-agent.jar";
81-
vm.loadAgent(agent);
82-
83-
// agent is started, get the connector address
84-
connectorAddress =
85-
vm.getAgentProperties().getProperty(CONNECTOR_ADDRESS);
86-
}
87-
88-
// establish connection to connector server
89-
// System.out.println(connectorAddress);
90-
serviceURL = new JMXServiceURL(connectorAddress);
9156

92-
} finally {
93-
vm.detach();
94-
}
95-
}
9657

9758
public JMXConnection(String serverUrl, String passwd) throws MalformedURLException {
9859

@@ -114,11 +75,11 @@ public String getConnectURL() {
11475
return connectURL;
11576
}
11677

117-
public MBeanServerConnection getServerConnection() throws MalformedURLException, IOException {
78+
public MBeanServerConnection getServerConnection() throws IOException {
11879
if (server == null) {
11980

120-
Map env = new HashMap();
121-
if (userName.length() > 0) {
81+
Map<String,String[]> env = new HashMap<>();
82+
if (!userName.isEmpty()) {
12283
String[] creds = {userName, userPassword};
12384
env.put(JMXConnector.CREDENTIALS, creds);
12485
}
@@ -136,9 +97,7 @@ public MBeanServerConnection getServerConnection() throws MalformedURLException,
13697
RUNTIME = new ObjectName("java.lang:type=Runtime");
13798
GC = new ObjectName("java.lang:type=GarbageCollector,name=*");
13899
THREADING = new ObjectName("java.lang:type=Threading");
139-
} catch (MalformedObjectNameException ex) {
140-
Logger.getLogger(JMXConnection.class.getName()).log(Level.SEVERE, null, ex);
141-
} catch (NullPointerException ex) {
100+
} catch (MalformedObjectNameException | NullPointerException ex) {
142101
Logger.getLogger(JMXConnection.class.getName()).log(Level.SEVERE, null, ex);
143102
}
144103
}
@@ -149,59 +108,39 @@ public long getUptime() {
149108

150109
l = (Long) getServerConnection().getAttribute(JMXConnection.RUNTIME, "Uptime");
151110

152-
} catch (MBeanException ex) {
153-
Logger.getLogger(JMXConnection.class.getName()).log(Level.SEVERE, null, ex);
154-
} catch (AttributeNotFoundException ex) {
155-
Logger.getLogger(JMXConnection.class.getName()).log(Level.SEVERE, null, ex);
156-
} catch (InstanceNotFoundException ex) {
157-
Logger.getLogger(JMXConnection.class.getName()).log(Level.SEVERE, null, ex);
158-
} catch (ReflectionException ex) {
159-
Logger.getLogger(JMXConnection.class.getName()).log(Level.SEVERE, null, ex);
160-
} catch (IOException ex) {
111+
} catch (MBeanException | IOException | ReflectionException | InstanceNotFoundException |
112+
AttributeNotFoundException ex) {
161113
Logger.getLogger(JMXConnection.class.getName()).log(Level.SEVERE, null, ex);
162114
}
163115
return l;
164116
}
165117

166-
public static float inSecsTimestamp(long ts) {
167-
return ((float) ts) / 1000;
168-
169-
}
170-
171-
boolean isUseAuthentication() {
172-
return !userName.isEmpty();
173-
}
174-
175118
public CompositeData[] getThreads(long[] thIds, int stackTraceEntriesNo) throws Exception {
176119
String[] signature = {"[J", "int"};
177120
Object[] params = {thIds, stackTraceEntriesNo};
178-
CompositeData[] threads = (CompositeData[]) server.invoke(THREADING, "getThreadInfo", params, signature);
179-
return threads;
121+
return (CompositeData[]) server.invoke(THREADING, "getThreadInfo", params, signature);
180122
}
181123

182124
public long[] getThreadsCPU(long[] thIds) throws Exception {
183125

184126
String[] signature = {"[J"};
185127
Object[] params = {thIds};
186-
long[] threadsCPU = (long[]) server.invoke(THREADING, "getThreadCpuTime", params, signature);
187-
return threadsCPU;
128+
return (long[]) server.invoke(THREADING, "getThreadCpuTime", params, signature);
188129
}
189130

190131
public long getThreadCPU(long thId) throws Exception {
191132

192133
String[] signature = {"long"};
193134
Object[] params = {thId};
194-
long threadCPU = (Long) server.invoke(THREADING, "getThreadCpuTime", params, signature);
195-
return threadCPU;
135+
return (long) (Long) server.invoke(THREADING, "getThreadCpuTime", params, signature);
196136

197137
}
198138

199139
public long[] getThreadsAllocBytes(long[] thIds) throws Exception {
200140

201141
String[] signature = {"[J"};
202142
Object[] params = {thIds};
203-
long[] threadsAllocBytes = (long[]) server.invoke(THREADING, "getThreadAllocatedBytes", params, signature);
204-
return threadsAllocBytes;
143+
return (long[]) server.invoke(THREADING, "getThreadAllocatedBytes", params, signature);
205144
}
206145
private boolean supportAdvFeatures = true;
207146

@@ -213,47 +152,4 @@ public void unsetJava16_25andAbove() {
213152
supportAdvFeatures = false;
214153
}
215154

216-
public static Class addToolsJar() {
217-
try {
218-
return com.sun.tools.attach.VirtualMachine.class;
219-
} catch (Throwable t) {
220-
System.out.println("tools.jar not in class path from"+ System.getProperty("java.home"));
221-
File toolsJar = new File(System.getProperty("java.home") + "/lib/tools.jar"); //when jdk
222-
System.out.println("try:" + toolsJar);
223-
if (toolsJar.exists()) {
224-
addURL(toolsJar);
225-
System.out.println(toolsJar);
226-
} else {
227-
toolsJar = new File(System.getProperty("java.home") + "/../lib/tools.jar"); // when jre part of jdk
228-
System.out.println("try:" + toolsJar);
229-
if (toolsJar.exists()) {
230-
addURL(toolsJar);
231-
System.out.println("Found:"+toolsJar);
232-
} else {
233-
System.out.println("Unable to locate tools.jar pls add it to classpath");
234-
}
235-
}
236-
}
237-
return com.sun.tools.attach.VirtualMachine.class;
238-
239-
240-
241-
}
242-
243-
public static void addURL(File file) throws RuntimeException {
244-
try {
245-
URL url = file.toURL();
246-
URLClassLoader classLoader = (URLClassLoader) ClassLoader.getSystemClassLoader();
247-
Class clazz = URLClassLoader.class;
248-
249-
// Use reflection
250-
Method method = clazz.getDeclaredMethod("addURL", new Class[]{URL.class});
251-
method.setAccessible(true);
252-
method.invoke(classLoader, new Object[]{url});
253-
254-
} catch (Exception e) {
255-
throw new RuntimeException(e);
256-
257-
}
258-
}
259155
}

src/main/java/com/performizeit/threadtop/Main.java

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,20 @@
2222

2323
public class Main {
2424

25-
public static void main(String args[]) throws Exception {
25+
public static void main(String[] args) throws Exception {
2626
if (args.length < 1) {
2727
String cliMessage = CliFactory.createCli(ThreadTopOptions.class).getHelpMessage();
28-
String correctMessage = cliMessage.replace("ARGUMENTS...", "<ProcessId> | <hostname:port> | <ip:port>" );
28+
String correctMessage = cliMessage.replace("ARGUMENTS...", "<hostname:port> | <ip:port>" );
2929
System.out.println(correctMessage);
30-
System.out.println("Threadtop can connect to a process on the same machine with pid\n"+
31-
"And via JMX port for a remote process.\n"+
32-
"To enable remote JMX and the following command line params:\n " +
33-
"-Dcom.sun.management.jmxremote\n " +
34-
"-Dcom.sun.management.jmxremote.port=[port]\n " +
35-
"-Dcom.sun.management.jmxremote.authenticate=false\n " +
36-
"-Dcom.sun.management.jmxremote.ssl=false\n");
30+
System.out.println("""
31+
Threadtop can connect to a process on the same machine with pid
32+
And via JMX port for a remote process.
33+
To enable remote JMX and the following command line params:
34+
-Dcom.sun.management.jmxremote
35+
-Dcom.sun.management.jmxremote.port=[port]
36+
-Dcom.sun.management.jmxremote.authenticate=false
37+
-Dcom.sun.management.jmxremote.ssl=false
38+
""");
3739
System.exit(1);
3840
}
3941

@@ -43,8 +45,8 @@ public static void main(String args[]) throws Exception {
4345

4446
ThreadTopOptions opts = CliFactory.parseArguments(ThreadTopOptions.class, args);
4547
String passwd = opts.getPassword();
46-
ArrayList<JMXConnection> servers = new ArrayList<JMXConnection>();
47-
if (opts.getConectionStringList().size() ==0) {
48+
ArrayList<JMXConnection> servers = new ArrayList<>();
49+
if (opts.getConectionStringList().isEmpty()) {
4850
System.out.println("Missing process id or host:port to connect");
4951
System.out.println(CliFactory.createCli(ThreadTopOptions.class).getHelpMessage());
5052
System.out.println("Enable remote JMX:\n -Dcom.sun.management.jmxremote\n -Dcom.sun.management.jmxremote.port=[port]\n -Dcom.sun.management.jmxremote.authenticate=false\n -Dcom.sun.management.jmxremote.ssl=false\n");
@@ -53,13 +55,7 @@ public static void main(String args[]) throws Exception {
5355
for (String hostPortUser : opts.getConectionStringList()) {
5456

5557

56-
JMXConnection server;
57-
try {
58-
Integer.parseInt(hostPortUser);
59-
server = new JMXConnection(hostPortUser);
60-
} catch (NumberFormatException e) {
61-
server = new JMXConnection(hostPortUser, passwd);
62-
}
58+
JMXConnection server = new JMXConnection(hostPortUser, passwd);
6359

6460
servers.add(server);
6561
}

0 commit comments

Comments
 (0)