2016-11-24 56 views
1

我試圖重新在威爾的blog post描述的行爲,但收到以下異常儀表上試圖通過運行它:異常的Java代理的調用通過ByteBuddy

$ java -javaagent:agent/target/securityfixer-agent-1.0-SNAPSHOT.jar=bootstrap/target/securityfixer-bootstrap-1.0-SNAPSHOT.jar -jar example/target/securi 
tyfixer-example-1.0-SNAPSHOT.jar                               
Exception in thread "main" java.lang.NoClassDefFoundError: net/bytebuddy/implementation/Implementation$Context$Factory          
     at java.lang.Class.getDeclaredMethods0(Native Method)                        
     at java.lang.Class.privateGetDeclaredMethods(Unknown Source)                      
     at java.lang.Class.getDeclaredMethod(Unknown Source)                        
     at sun.instrument.InstrumentationImpl.loadClassAndStartAgent(Unknown Source)                  
     at sun.instrument.InstrumentationImpl.loadClassAndCallPremain(Unknown Source)                  
Caused by: java.lang.ClassNotFoundException: net.bytebuddy.implementation.Implementation$Context$Factory             
     at java.net.URLClassLoader.findClass(Unknown Source)                        
     at java.lang.ClassLoader.loadClass(Unknown Source)                         
     at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)                      
     at java.lang.ClassLoader.loadClass(Unknown Source)                         
     ... 5 more                                   
FATAL ERROR in native method: processing of -javaagent failed 

structure是在描述威爾的博客 - 三個獨立的罐子,一個與代理人,一個與攔截器和一個與主類。

我也試着運行它通過將mainClass節到securityfixer-例子的清單可執行的JAR,但是,這似乎是完全繞過儀表:

$ java -jar example/target/securityfixer-example-1.0-SNAPSHOT.jar -javaagent:agent/target/securityfixer-agent-1.0-SNAPSHOT.jar=bootstrap/target/securityfixer-bootstrap-1.0-SNAPSHOT.jar 
Security manager is set! 
ATTACK SUCCEEDED: Security manager was reset! 

什麼可能我在這裏失去了?提前致謝。

回答

1

下面的設置似乎工作:

byte-buddy-1.0.0.jar必須是內java-agents-experiments\securityfixer\agent\target與genarated securityfixer-agent-1.0-SNAPSHOT.jar一起,因爲後者依賴於前者。這是通過包括以下的插件,其執行復制,在securityfixer-agent/pom.xml實現:

  <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-dependency-plugin</artifactId> 
       <version>2.8</version> 
       <executions> 
        <execution> 
         <id>copy-dependencies</id> 
         <phase>prepare-package</phase> 
         <goals> 
          <goal>copy-dependencies</goal> 
         </goals> 
         <configuration> 
          <outputDirectory>${project.build.directory}</outputDirectory> 
          <includeScope>runtime</includeScope> 
         </configuration> 
        </execution> 
       </executions> 
      </plugin> 

以及在<Boot-Class-Path>到工件由上述節產生以下參考:

 <plugin> 
      <artifactId>maven-jar-plugin</artifactId> 
      <version>2.4</version> 
      <configuration> 
       <archive> 
        <manifestEntries> 
         <Can-Redefine-Classes>true</Can-Redefine-Classes> 
         <Can-Retransform-Classes>true</Can-Retransform-Classes> 
         <Agent-Class>com.excelsiorsoft.securityfixer.agent.SecurityFixerAgent</Agent-Class> 
         <Premain-Class>com.excelsiorsoft.securityfixer.agent.SecurityFixerAgent</Premain-Class> 
         <Boot-Class-Path>byte-buddy-1.0.0.jar</Boot-Class-Path> 
        </manifestEntries> 
       </archive> 
      </configuration> 
     </plugin> 

使不必要的依賴關係(如securityfixer-bootstrap)不會被上面的插件複製,byte-buddy-1.0.0.jar我需要將其範圍更改爲providedmaven-dependency-plugin似乎跳過將該範圍的依賴關係複製到其目標文件夾。

爲了能夠運行爲可執行的JAR文件,我們需要將<mainClass>節添加到securityfixer-example/pom.xml

 <plugin> 
      <artifactId>maven-jar-plugin</artifactId> 
      <version>2.4</version> 
      <configuration> 
       <archive> 
        <manifest> 
         <mainClass>securityfixer.Main</mainClass> 
        </manifest> 
       </archive> 
      </configuration> 
     </plugin> 

$ java -javaagent:agent/target/securityfixer-agent-1.0-SNAPSHOT.jar=bootstrap/target/securityfixer-bootstrap-1.0-SNAPSHOT.jar -jar example/target/securit yfixer-example-1.0-SNAPSHOT.jar 


Security manager is set!  
ATTACK FAILED: SecurityManager cannot be reset! 

隨意評論 - 也許有一個更優雅的解決方案。謝謝!

+0

對於那些遇到此問題的用戶,請務必使用Byte Buddy版本1.4.1或更低版本。從1.4.2開始,它不再有效......仍然試圖追查爲什麼! –

+0

你用ver獲得的錯誤是什麼。 1.4.2? –