Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

290
Visualizações
Load a Java agent JAR located inside the loader

I have two separate projects in my IDE for the agent and for the loader that finds a target VM and loads the agent JAR.

  • When the agent project is built, the resulting agent JAR artifact is copied into the loader's resources folder.
  • When the loader project is built, the loader JAR contains both loader code and the agent.jar in it.

The resulting runnable loader structure looks like this:

loader.jar
├── META-INF
│   └── MANIFEST.MF
├── me.domain.loader
│   └── Main.class
└── agent.jar
    ├── META-INF
    │   └── MANIFEST.MF
    └── me.domain.agent
        └── Agent.class

From the VirtualMachine#loadAgent(java.lang.String) specification, I need to provide a path to the JAR containing the agent as the first parameter.

However, when using Main.class.getResource("/agent.jar").getPath() I'm getting an AgentLoadException: Agent JAR not found or no Agent-Class attribute. What's the correct way to do that?

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

I already had such issue on a maven project. Anyway you may need to have a manifest file here in META-INF/MANIFEST.MF:

Manifest-Version: 1.0
Agent-Class: com.package.AgentLoader.agentNameHere
Permissions: all-permissions

You have more details here: https://docs.oracle.com/javase/tutorial/deployment/jar/manifestindex.html or Agent JAR not found or no Agent-Class attribute

over 4 years ago · Santiago Trujillo Relatório

0

It looks like the agent JAR to be loaded must exist on disk. I've solved this issue by copying the embedded JAR resource into a temporary file:

private static String getTemporaryResource(String resourceName) {

    // Read embedded resource from this JAR
    InputStream resourceStream = Main.class.getResourceAsStream(resourceName);
    if (resourceStream == null) {
        throw new Exception("Resource not found in the JAR");
    }

    // Create a temporary file in %TEMP%/resource5513111026806316867.tmp
    File temporaryFile = File.createTempFile("resource", null);
    temporaryFile.deleteOnExit();

    // Copy the resource data into the temporary file
    Files.copy(resourceStream, temporaryFile.toPath(), StandardCopyOption.REPLACE_EXISTING);

    // Return the path to temporary file
    return temporaryFile.getAbsolutePath();
}

I'm then using this temporary path to load the agent:

String tempAgentPath = getTemporaryResource("/agent.jar");
VirtualMachine targetVM = VirtualMachine.attach("1337");
targetVM.loadAgent(tempAgentPath);
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda