微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

NoClassDefFoundError:com / google / cloud / kms / v1 / KeyManagementServiceClient

如何解决NoClassDefFoundError:com / google / cloud / kms / v1 / KeyManagementServiceClient

在这里第一次写作,所以我对这个问题要补充表示歉意。我正在使用Java创建应用程序,并尝试在Google Cloud上创建KeyManagementServiceClient连接。该程序正确构建,但是当我运行以下功能时,我收到与com / google / cloud / kms / v1 / KeyManagementServiceClient相关的NoClassDefFound错误

try (KeyManagementServiceClient client = KeyManagementServiceClient.create())

我正在使用Maven,对这个问题的所有研究都使我认为我的pom.xml文件存在问题,下面已经包含了该文件,但是尝试进行任何修复都不能摆脱错误

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.mycompany.app</groupId>
  <artifactId>encryptiontobucket</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>my-app</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>

<dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>com.google.cloud</groupId>
        <artifactId>google-cloud-kms-bom</artifactId>
        <version>1.40.0</version>
        <type>pom</type>
        <scope>import</scope>
       </dependency>
     </dependencies>
  </dependencyManagement>

  <dependencies>
    <dependency>
      <groupId>com.google.cloud</groupId>
      <artifactId>google-cloud-kms</artifactId>
    </dependency>
  </dependencies>

  <build>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <!-- clean lifecycle,see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- default lifecycle,jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-jar-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
        <!-- site lifecycle,see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
        <plugin>
          <artifactId>maven-site-plugin</artifactId>
          <version>3.7.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-project-info-reports-plugin</artifactId>
          <version>3.0.0</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

如果有人可以帮助我修复错误,将不胜感激!另外,请让我知道是否需要其他任何方式来理解该问题。

编辑:以下是我的代码。我最初创建了一个名为my-app的maven项目,该项目具有pom.xml。我的程序EncryptionToBucket.java位于maven认创建的app文件夹中的目录中。重现该问题:

  1. 将程序放在上面指示的位置。
  2. 运行mvn软件包
  3. 运行java -cp target \ my-app-1.0-SNAPSHOT.jar EncryptionToBucket
  4. 输入一个随机项目ID,选择一个随机locationId,然后单击“创建新密钥”按钮,该错误将在命令行上发生。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import com.google.cloud.kms.v1.CryptoKey;
import com.google.cloud.kms.v1.CryptoKey.CryptoKeyPurpose;
import com.google.cloud.kms.v1.Cryptokeyversion.CryptokeyversionAlgorithm;
import com.google.cloud.kms.v1.CryptokeyversionTemplate;
import com.google.cloud.kms.v1.KeyManagementServiceClient;
import com.google.cloud.kms.v1.KeyRing;
import com.google.cloud.kms.v1.LocationName;
import java.io.IOException;


class EncryptionToBucket implements ActionListener{
    private String projectId,locationId,keyRingPath,keyNamePath,asymmKey;
    private JButton createNewKeyRing,createNewKey,encryptData,back,back2,createNewSigningKey;
    private JTextField projText,keyRingTField,keyNameTField,asymmKeyEntry;
    private JComboBox locations;
    private JFrame initialFrame,keyRingFrame,keyFrame;

    public EncryptionToBucket(){
        initialInfoPage();
    }

    public void keyRingCreation() {
    // Initialize client that will be used to send requests. This client only
    // needs to be created once,and can be reused for multiple requests. After
    // completing all of your requests,call the "close" method on the client to
    // safely clean up any remaining background resources.
     try (KeyManagementServiceClient client = KeyManagementServiceClient.create()) {
      // Build the parent name from the project and location.
        // LocationName locationName = LocationName.of(projectId,locationId);

      // Build the key ring to create.
        //KeyRing keyRing = KeyRing.newBuilder().build();

      // Create the key ring.
        // KeyRing createdKeyRing = client.createKeyRing(locationName,asymmKey,keyRing);
         //System.out.printf("Created key ring %s%n",createdKeyRing.getName());
        }catch (IOException e){
            //System.out.println("Rut Row");
        }
  
    }
    

    public static void main(String args[]){
        EncryptionToBucket constructor = new EncryptionToBucket();
    }

    public void initialInfoPage(){
        /*Todo: Remove Gaps
        */
        initialFrame = new JFrame("Enter information");
        initialFrame.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);
        initialFrame.setSize(500,500);
        initialFrame.setResizable(false);
        initialFrame.setLocationRelativeto(null);

        JLabel proj = new JLabel("Project ID: ");
        JLabel location = new JLabel("Location ID: ");

        projText = new JTextField(10);
        projText.setText(projectId);
        String[] locationIds= {" ","asia-east1","asia-east2","asia-northeast1","asia-northeast2","asia-northeast3","asia-south1","asia-southeast1","asia-southeast2","australia-southeast1","europe-north1","europe-west1","europe-west2","europe-west3","europe-west4","europe-west6","northamerica-northeast1","southamerica-east1","us-central1","us-east1","us-east4","us-west1","us-west2","us-west3","us-west4"};
        locations = new JComboBox(locationIds);
        locations.setSelectedItem(locationId);

        GridLayout layout = new GridLayout(2,0);
        JPanel topPane = new JPanel(layout);
        JPanel topPane1 = new JPanel();
        JPanel topPane2 = new JPanel();
        topPane1.add(proj);
        topPane1.add(projText);
        topPane2.add(location);
        topPane2.add(locations);
        topPane.add(topPane1);
        topPane.add(topPane2);
        
         JPanel secondTierPane = new JPanel(new GridLayout(2,0));
         JPanel secondTierPane1 = new JPanel();
         JPanel secondTierPane2 = new JPanel();
         JLabel keyRingName = new JLabel("Key Ring Path: ");
         secondTierPane1.add(keyRingName);
         keyRingTField = new JTextField(10);
         keyRingTField.setText(keyRingPath);
         secondTierPane1.add(keyRingTField);
         secondTierPane.add(secondTierPane1);
         createNewKeyRing = new JButton("Create a New Key Ring");
         createNewKeyRing.addActionListener(this);
         secondTierPane2.add(createNewKeyRing);
         secondTierPane.add(secondTierPane2);


         JPanel thirdTierPane = new JPanel(new GridLayout(2,0));
         JPanel thirdTierPane1 = new JPanel();
         JPanel thirdTierPane2 = new JPanel();
         JLabel keyName = new JLabel("Key Path: ");
         thirdTierPane1.add(keyName);
         keyNameTField = new JTextField(10);
         keyNameTField.setText(keyNamePath);
         thirdTierPane1.add(keyNameTField);
         thirdTierPane.add(thirdTierPane1);
         createNewKey = new JButton("Create a New Key");
         createNewKey.addActionListener(this);
         thirdTierPane2.add(createNewKey);
         thirdTierPane.add(thirdTierPane2);

         JPanel bottomPane = new JPanel();
         encryptData = new JButton("Encrypt Data"); 
         bottomPane.add(encryptData);

         JPanel main = new JPanel(new GridLayout(4,1));

         main.add(topPane);
         main.add(secondTierPane);
         main.add(thirdTierPane);
         main.add(bottomPane);
        
        initialFrame.add(main);
        initialFrame.setVisible(true);
    }

    public void actionPerformed(ActionEvent e){
        
        if(e.getSource() == createNewKeyRing){
            projectId = projText.getText();
            locationId = locations.getSelectedItem().toString();
            //initialFrame.setVisible(false);
            keyRingPage();
        }else if(e.getSource() == createNewKey){
            if(projText.getText().isEmpty()){
                JOptionPane errormessage = new JOptionPane();
                errormessage.showMessageDialog(null,"A projectId is needed to create a key ring!","alert",JOptionPane.ERROR_MESSAGE);
            }else{
                projectId = projText.getText();
            }
            locationId = locations.getSelectedItem().toString();
            keyRingPath = keyRingTField.getText();
            initialFrame.setVisible(false);
            keyPage();
        }else if(e.getSource() == encryptData){

        }else if(e.getSource() == back || e.getSource() == back2){
            if(keyRingFrame.isVisible()){
                asymmKey = asymmKeyEntry.getText();
                keyRingFrame.setVisible(false);
                keyRingCreation();
            }else if(keyFrame.isVisible()){
                keyFrame.setVisible(false);
            }
            initialInfoPage();
        }
        
    }

    public void keyRingPage(){
    /*
    Todo: Key Ring Creation Code
    */

        keyRingFrame = new JFrame("Key Ring Creation");
        keyRingFrame.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);
        keyRingFrame.setSize(500,400);
        keyRingFrame.setResizable(true);
        keyRingFrame.setLocationRelativeto(null);

        JPanel main = new JPanel(new GridLayout (3,2));
        
        JPanel topPane = new JPanel();
        topPane.setLayout(new BoxLayout(topPane,BoxLayout.PAGE_AXIS));
        JLabel projectsId = new JLabel("Project ID: " + projectId);
        projectsId.setAlignmentX(Component.CENTER_ALIGNMENT);
        JLabel locationsId = new JLabel("Location ID: " + locationId);
        locationsId.setAlignmentX(Component.CENTER_ALIGNMENT);
        topPane.add(projectsId);
        topPane.add(locationsId);
        main.add(topPane);

        JPanel midPane = new JPanel();
        midPane.setLayout(new BoxLayout(midPane,BoxLayout.PAGE_AXIS));
        JLabel asymmKeyName = new JLabel("asymmetric Signing Key: ");
        midPane.add(asymmKeyName);
        asymmKeyEntry = new JTextField(10);
        //asymmKeyEntry.setPreferredSize();
        asymmKeyEntry.setAlignmentX(Component.CENTER_ALIGNMENT);
        midPane.add(asymmKeyEntry);
        createNewSigningKey = new JButton("Create New Signing Key");
        midPane.add(createNewSigningKey);
        main.add(midPane);

        JPanel lowPane = new JPanel();
        back = new JButton("Create Key Ring");
        back.addActionListener(this);
        lowPane.add(back);
        main.add(lowPane);

        keyRingFrame.add(main);
        keyRingFrame.setVisible(true);
    }

    public void keyPage(){
        keyFrame = new JFrame("Key Ring Creation");
        keyFrame.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);
        keyFrame.setSize(500,400);
        keyFrame.setResizable(true);
        keyFrame.setLocationRelativeto(null);

        JPanel main = new JPanel();
        back2 = new JButton("Enter");
        back2.addActionListener(this);
        main.add(back2);
        keyFrame.add(main);
        keyFrame.setVisible(true);
    }
    

}



解决方法

要安装客户端库,请查看下面的链接

https://cloud.google.com/kms/docs/reference/libraries#installing_the_client_library

如果使用的是Maven,请将以下内容添加到pom.xml文件中。

custom-1-15360-ext
,

我也遇到了这个问题。

使用以下解决了我的问题:

        <dependency>
            <groupId>com.google.cloud</groupId>
            <artifactId>google-cloud-kms</artifactId>
            <version>0.72.0-beta</version>
        </dependency>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。