Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

稍微提高了一点用户体验: #166

Merged
merged 1 commit into from
May 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ public static String parsePid(OptionParser parser, OptionSet optionSet) {
pid = Integer.valueOf((String) optionSet.nonOptionArguments().get(0));
}

if (pid == null){
pid = SelectPid.getPidFromJpsList();
}

if (pid == null) {
System.out.println("PID can't be empty !!!");
VJTop.printHelper(parser);
Expand Down
62 changes: 62 additions & 0 deletions vjtop/src/main/java/com/vip/vjtools/vjtop/util/SelectPid.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.vip.vjtools.vjtop.util;

import java.io.BufferedReader;
import java.io.Console;
import java.io.InputStreamReader;

/**
* Created by traburiss on 2019/12/11.
* describe:
*/
public class SelectPid {

public static Integer getPidFromJpsList(){

Integer pid = null;
Process process = null;
BufferedReader reader = null;
try {

Console console = System.console();
process = Runtime.getRuntime().exec("jps -l");
reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String defPidStr = "";
String line;
System.out.println("please input a pid from list:");
System.out.println("PID\tNAME");
while ((line = reader.readLine()) != null) {
//不显示jps和自己
if (!line.contains("sun.tools.jps.Jps") && !line.contains("com.vip.vjtools.vjtop.VJTop")){
String[] pm = line.split(" ",2);
if (defPidStr.isEmpty()){
defPidStr = pm[0];
}
System.out.println(pm[0] + "\t" + pm[1]);
}
}
String pidString = console.readLine("\nplease input pid(default is " + defPidStr + "):");
if (pidString == null || pidString.isEmpty()){
pidString = defPidStr;
}
pid = Integer.valueOf(pidString);
}catch (Exception ignored){
}finally {

try {
if (reader != null) {
reader.close();
}
} catch (Exception e) {
e.printStackTrace();
}
try {
if (process != null) {
process.destroy();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return pid;
}
}