【问题标题】:Caused by: java.lang.ClassNotFoundException: Didn't find class "java.awt.Desktop" in Android原因:java.lang.ClassNotFoundException:在 Android 中找不到类“java.awt.Desktop”
【发布时间】:2018-03-12 11:34:54
【问题描述】:

我在 android 中运行 java 代码以使用 android 中的 oauth 2.0 制作凭据,当我运行此代码时遇到问题:

这是我的问题:

原因:java.lang.ClassNotFoundException:在路径上找不到类“java.awt.Desktop”:DexPathList[[zip file“/data/app/com.retrofitdemo-1/base.apk”], nativeLibraryDirectories=[/vendor/lib64, /system/lib64]]

这是我的代码:

 private static final GoogleClientSecrets googleSecrets = new GoogleClientSecrets();
 googleSecrets.setInstalled(
            new GoogleClientSecrets.Details()
                    .setClientId("542678605378-hlspiumlr34nq04cmjtkft17i6k3hvr0.apps.googleusercontent.com")
                    .setClientSecret("{client_secretid}")
    );
    // Build flow and trigger user authorization request.
    GoogleAuthorizationCodeFlow flow =
            new GoogleAuthorizationCodeFlow.Builder(
                    AndroidHttp.newCompatibleTransport(), JacksonFactory.getDefaultInstance(), googleSecrets, Arrays.asList(SCOPES))
 .setAccessType("offline")
                    .build();
    Credential credential1 = new AuthorizationCodeInstalledApp(
            flow, new LocalServerReceiver()).authorize("user");
    Log.i("AA","credential1--"+credential1);

    new MakeRequestTask(credential).execute();

如何在android中导入awt包?

【问题讨论】:

  • 我也有这个问题,你解决成功了吗?

标签: android


【解决方案1】:

请帮帮我,如何在android中导入awt包

你不能。据推测,您正在使用一些专为桌面 Java 而不是 Android 设计的库。查找一些专为 Android 设计的替换库。

【讨论】:

  • 您有什么想法可以替换其他库,请告诉我
  • @kalaivani:您没有在问题中提供完整的 Java 堆栈跟踪。结果,我不知道什么代码在尝试使用 AWT。
【解决方案2】:

还不允许发表评论,所以这不是答案,但也许有人可以将其添加为评论...

这是请求的堆栈跟踪示例。可以看出这是 oauth 的 Google 代码:

    java.lang.NoClassDefFoundError: Failed resolution of: Ljava/awt/Desktop;
    at com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp.browse(AuthorizationCodeInstalledApp.java:129)
    at com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp.onAuthorization(AuthorizationCodeInstalledApp.java:113)
    at com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp.authorize(AuthorizationCodeInstalledApp.java:81)

以及尝试与 Desktop 类对话的代码:

   /**
   * Open a browser at the given URL using {@link Desktop} if available, or alternatively output the
   * URL to {@link System#out} for command-line applications.
   *
   * @param url URL to browse
   */
  public static void browse(String url) {
    Preconditions.checkNotNull(url);
    // Ask user to open in their browser using copy-paste
    System.out.println("Please open the following address in your browser:");
    System.out.println("  " + url);
    // Attempt to open it in the browser
    try {
      if (Desktop.isDesktopSupported()) {
        Desktop desktop = Desktop.getDesktop();
        if (desktop.isSupported(Action.BROWSE)) {
          System.out.println("Attempting to open that address in the default browser now...");
          desktop.browse(URI.create(url));
        }
      }

【讨论】:

  • 这篇文章并不是回答问题的实际尝试。请注意 SOdoesn't work like a discussion forum,这是一个问答网站,每个帖子都是问题或问题的答案。如果您有类似的问题,请提出一个新问题,并包含指向该问题的链接以帮助提供上下文。见How do I ask a good question?。这将大大增加您获得答案的机会。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-03-31
  • 2021-02-11
  • 1970-01-01
  • 1970-01-01
  • 2021-11-02
  • 2016-12-19
  • 2019-08-22
相关资源
最近更新 更多