当前位置: 首页 > 知识库问答 >
问题:

MacOS上的Java将外部USB驱动器视为不可读-所有其他应用程序将其视为可读

鲜于俊侠
2023-03-14

Mac OSx(Macbook Pro M1,Monterey 12.3)上的Java(8和17)认为外部USB驱动器不可读。这曾经奏效,相信问题始于12.3的安装。下面的测试例程显示了两个文件(..)。canRead()和文件。可阅读(…)返回false。所有其他应用程序(Finder、terminal、Time Machine)都使用相同的驱动器,没有任何问题。问题报告提交给了苹果,但还有其他人看到过这个问题吗?

注意:如果驱动器不可读,则使用文件(..)。listFiles()返回NULL和文件。列表(…)抛出异常。

import java.nio.file.*;
import java.io.*;

/** Test routine for failure to access external USB drive (USBExtA in testPath) from Java.
 *
 *  The testPath is attempting to access Time Machine backups on the external drive, which shows as not readable in Java.
 *  BUT IT IS READABLE with no problems from Finder, the terminal, and other applications. Time Machine is still
 *  successfully writing backups to this drive.
 *
 *  THIS USED TO WORK -- believe that it stopped working with installation of Monterey 12.3, and took a while
 *                       before I noticed the problem (but not exactly sure of the date when 12.3 installed).
 *
 *  ----------- Output of executions using Java 8 and Java 17 ------------------
 *
 * --------- Execution Context ----------
 *       java.version -- 1.8.0_311
 *        java.vendor -- Oracle Corporation
 *    java.vm.version -- 25.311-b11
 *     java.vm.vendor -- Oracle Corporation
 * java.class.version -- 52.0
 *            os.name -- Mac OS X
 *            os.arch -- x86_64
 *         os.version -- 10.16
 *
 * java.io.File -- Exists:  T , Readable:  T , Dir:  T , java.nio.Files -- Exists:  T , Readable:  T , Dir:  T  --- PATH: /Volumes
 * java.io.File -- Exists:  T , Readable: *F*, Dir:  T , java.nio.Files -- Exists:  T , Readable: *F*, Dir:  T  --- PATH: /Volumes/USBExtA
 *
 *  --------- Execution Context ----------
 *       java.version -- 17.0.1
 *        java.vendor -- Oracle Corporation
 *    java.vm.version -- 17.0.1+12-LTS-39
 *     java.vm.vendor -- Oracle Corporation
 * java.class.version -- 61.0
 *            os.name -- Mac OS X
 *            os.arch -- aarch64
 *         os.version -- 12.3
 *
 * java.io.File -- Exists:  T , Readable:  T , Dir:  T , java.nio.Files -- Exists:  T , Readable:  T , Dir:  T  --- PATH: /Volumes
 * java.io.File -- Exists:  T , Readable: *F*, Dir:  T , java.nio.Files -- Exists:  T , Readable: *F*, Dir:  T  --- PATH: /Volumes/USBExtA
 */
public class ExtUSBTest {

  public static String testPath = "/Volumes/USBExtA/Backups.backupdb/JDCMacBook";

  public static void main(String[] args) {
    showContext("java.version", "java.vendor", "java.vm.version", "java.vm.vendor", "java.class.version",
                "os.name", "os.arch", "os.version");

    checkPath(Paths.get(testPath));
  }

  /** Recursively check all sub-components of the given path - stops if not exists && readable */
  public static boolean checkPath(Path path) {
    if(path.getParent() == null)
      return true;
    else if(checkPath(path.getParent()))
      return checkThisPath(path);
    else
      return false;
  }
  /** Check this particular path to see if exists & readable */
  public static boolean checkThisPath(Path path) {
    File f            = path.toFile();
    boolean fExists   = f.exists();
    boolean fReadable = f.canRead();
    boolean fDir      = f.isDirectory();
    boolean pExists   = Files.exists(path);
    boolean pReadable = Files.isReadable(path);
    boolean pDir      = Files.isDirectory(path);

    String rslt = String.format("java.io.File -- Exists: %s, Readable: %s, Dir: %s, java.nio.Files -- Exists: %s, Readable: %s, Dir: %s --- PATH: %s",
                                  bstr(fExists), bstr(fReadable), bstr(fDir), bstr(pExists), bstr(pReadable), bstr(pDir), path.toString());
    ln(rslt);
    return (fExists && fReadable) || (pExists && pReadable);
  }
  public static void showContext(String ... props) {
    ln("--------- Execution Context ----------");
    for(int i = 0; i<props.length; i++) showProp(props[i]);
    ln("");
  }
  public static void showProp(String property) {
    ln(String.format("%18s -- %s", property, System.getProperty(property)));
  }
  public static String bstr(boolean b) { return b ? " T " : "*F*";}

  public static void ln(String s) { System.out.println(s); }
}

共有1个答案

上官迪
2023-03-14

当我在Monterey 12.3上运行M1 Pro Mac时,我看到了以下输出。所以,我不认为这是特定于最新版本的蒙特利(12.3)!

    **--------- Execution Context ----------
      java.version -- 17.0.2
       java.vendor -- Amazon.com Inc.
   java.vm.version -- 17.0.2+8-LTS
    java.vm.vendor -- Amazon.com Inc.
java.class.version -- 61.0
           os.name -- Mac OS X
           os.arch -- aarch64
        os.version -- 12.3
java.io.File -- Exists:  T , Readable:  T , Dir:  T , java.nio.Files -- Exists:  T , Readable:  T , Dir:  T  --- PATH: /Volumes
java.io.File -- Exists:  T , Readable:  T , Dir:  T , java.nio.Files -- Exists:  T , Readable:  T , Dir:  T  --- PATH: /Volumes/SSD1
java.io.File -- Exists:  T , Readable:  T , Dir:  T , java.nio.Files -- Exists:  T , Readable:  T , Dir:  T  --- PATH: /Volumes/SSD1/cp**
 类似资料:
  • 我有一些奇怪的影响,当试图动画视图从消失到可见。见附件gif。 我有一个简单的,它切换的可见性。当更改为可见时,上会出现不希望的类似动画的影响。 我认为这与布局高度有关。请参见下面的布局。 当我删除,并将布局高度更改为时,它工作正常。但这不是解决方案,因为我最终需要一个。 如何避免按钮动画效果? 源代码:

  • 我有一个应用程序,可以让用户设置特定时间期限的任务。一旦过了最后期限,我希望应用程序在所有内容上显示一个视图(请求用户更新任务)。 我的问题是:-如何在当前活动的所有其他活动上显示我的应用程序的视图-如何在用户更新任务之前保持活动在顶部? 使用系统警报窗口这样的东西看起来合理吗?

  • 我在Mac OS Big Sur 11.4上使用zsh,我尝试在格式的外部usb驱动器上执行chmod。但是,它没有像预期的那样工作。为什么?

  • 我必须在Springboot java应用程序中的多个位置调用外部api。外部api将始终只返回静态常量字符串值。 请查找下面的示例代码,以便更好地解释我的意图以及我希望在一天结束时实现的目标 我的示例代码使用RestTemplate调用外部api来检索字符串值。 现在,我的目的是使这个字符串在应用程序中全局可用,以避免多次调用这个api。 我正在考虑在应用程序启动时调用这个扩展api,并在Spr

  • 问题内容: 看到这个答案。它说: 六个非常糟糕的例子; … 锁定在可变字段上。例如,synced(object){object = …; } 锁定可变字段有什么问题?如果被声明为但不是不可变的类怎么办? 问题答案: 这是一个坏主意,因为如果另一个线程更改了关键部分中的引用,则这些线程将不再看到相同的引用,因此它们将不会在同一对象上同步,从而不受控制地运行。例: 假设有2个线程试图进入此关键部分。线

  • 我有一个视图想要折叠,所以我将可见性设置为view,该视图变得不可见,但它仍然占用空间。 我不知道可能是什么,有人有想法? 下面是布局: 提前致谢