imrichardcole
imrichardcole

Reputation: 4695

Get list of dlls loaded by Java process

I've been spending a lot of time with java process error files i.e hs_err_pid*.log. In these files is a section titled Dynamic libraries that shows all the dlls that were currently loaded by the process at the time it failed.

Is is possible to programmatically get access to this information during normal running conditions?

Upvotes: 2

Views: 1971

Answers (3)

Daniel
Daniel

Reputation: 28084

At least in JDK 8 times it was possible to do it this way:

@SuppressWarnings("unchecked")
public static Set<String> getLoadedLibraryNames() throws Exception {
    Set<String> allLibs = new TreeSet<String>();
    Field loadedLibraryNames = ClassLoader.class.getDeclaredField("loadedLibraryNames");
    loadedLibraryNames.setAccessible(true);
    allLibs.addAll((List<String>) loadedLibraryNames.get(SystemUtils.class.getClassLoader()));
    allLibs.addAll((List<String>) loadedLibraryNames.get(ClassLoader.getSystemClassLoader()));
    return allLibs;
}

In case someone found out how to do this in JDK 17, you would make me very happy.

Upvotes: 0

MK.
MK.

Reputation: 34587

https://msdn.microsoft.com/en-us/library/windows/desktop/ms682621%28v=vs.85%29.aspx

#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#include <psapi.h>

// To ensure correct resolution of symbols, add Psapi.lib to TARGETLIBS
// and compile with -DPSAPI_VERSION=1

int PrintModules( DWORD processID )
{
    HMODULE hMods[1024];
    HANDLE hProcess;
    DWORD cbNeeded;
    unsigned int i;

    // Print the process identifier.

    printf( "\nProcess ID: %u\n", processID );

    // Get a handle to the process.

    hProcess = OpenProcess( PROCESS_QUERY_INFORMATION |
                            PROCESS_VM_READ,
                            FALSE, processID );
    if (NULL == hProcess)
        return 1;

   // Get a list of all the modules in this process.

    if( EnumProcessModules(hProcess, hMods, sizeof(hMods), &cbNeeded))
    {
        for ( i = 0; i < (cbNeeded / sizeof(HMODULE)); i++ )
        {
            TCHAR szModName[MAX_PATH];

            // Get the full path to the module's file.

            if ( GetModuleFileNameEx( hProcess, hMods[i], szModName,
                                      sizeof(szModName) / sizeof(TCHAR)))
            {
                // Print the module name and handle value.

                _tprintf( TEXT("\t%s (0x%08X)\n"), szModName, hMods[i] );
            }
        }
    }

    // Release the handle to the process.

    CloseHandle( hProcess );

    return 0;
}

int main( void )
{

    DWORD aProcesses[1024]; 
    DWORD cbNeeded; 
    DWORD cProcesses;
    unsigned int i;

    // Get the list of process identifiers.

    if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) )
        return 1;

    // Calculate how many process identifiers were returned.

    cProcesses = cbNeeded / sizeof(DWORD);

    // Print the names of the modules for each process.

    for ( i = 0; i < cProcesses; i++ )
    {
        PrintModules( aProcesses[i] );
    }

    return 0;
}

Upvotes: 1

Harpal
Harpal

Reputation: 135

goto jdk\bin path in command prompt and run below commond.

jmap (process id)

this will print all dlls loaded in tht process. not sure about programtically. this works. i m using for automation purpose.

Note: Your JDK and JRE version should be match(compactible)

Upvotes: 1

Related Questions