public ProcessDescription findProcess ( Pattern pPattern ) throws InvocationTargetException , NoSuchMethodException , IllegalAccessException { List < ProcessDescription > ret = new ArrayList < ProcessDescription > ( ) ; String ownId = getOwnProcessId ( ) ; for ( ProcessDescription desc : listProcesses ( ) ) { Matcher matcher = pPattern . matcher ( desc . getDisplay ( ) ) ; if ( ! desc . getId ( ) . equals ( ownId ) && matcher . find ( ) ) { ret . add ( desc ) ; } } if ( ret . size ( ) == 1 ) { return ret . get ( 0 ) ; } else if ( ret . size ( ) == 0 ) { throw new IllegalArgumentException ( "No attachable process found matching \"" + pPattern . pattern ( ) + "\"" ) ; } else { StringBuilder buf = new StringBuilder ( ) ; for ( ProcessDescription desc : ret ) { buf . append ( desc . getId ( ) ) . append ( " (" ) . append ( desc . getDisplay ( ) ) . append ( ")," ) ; } throw new IllegalArgumentException ( "More than one attachable process found matching \"" + pPattern . pattern ( ) + "\": " + buf . substring ( 0 , buf . length ( ) - 1 ) ) ; } }
Filter the process list for a regular expression and returns the description . The process this JVM is running in is ignored . If more than one process or no process is found an exception is raised .