hug*_*411 6 java macos objective-c
我需要/Library/Application Support/在 Java 应用程序中将数据保存在macOS 内。要访问用户应用程序数据,我知道我可以使用,System.getProperty("user.home")但我想要来自根用户的数据,而不是用户。我看到一些应用程序在其中存储数据,有没有办法在 java 中做到这一点?
如果不是在 java 中,我正在加载 obj-c 库,是否可以在 obj-c 中提升权限,以便我的 java 应用程序可以访问该文件夹?
如果这是一个交互式程序,那么使用authopen将是完美的,否则我会使用launchd。
\n\n我刚刚发现这个答案已经被删除了。为什么有人要删除正确答案?
\n\n下面是一个简化的示例来说明如何使用 authopen 来回答问题:
\n\npackage macos4;\n\nimport java.io.File;\nimport java.io.IOException;\n\npublic class MacOS4 {\n\n public static void main(String[] args) throws IOException, InterruptedException {\n String destinationFileName = "/Library/Application Support/tempFile/";\n ProcessBuilder builder = new ProcessBuilder("/usr/libexec/authopen", "-c", "-w", "-a", destinationFileName);\n builder.redirectInput(new File("src/resources", "input.txt"));\n builder.redirectError(ProcessBuilder.Redirect.INHERIT);\n builder.redirectOutput(ProcessBuilder.Redirect.INHERIT);\n Process process = builder.start();\n process.waitFor();\n process.destroy();\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n\n如果tempFile不存在,这将创建它,并将资源包中input.txt的内容写入其中。它将弹出进行身份验证:\n
\n这意味着不需要将密码通过管道传递给子进程。
有关需要防止外部进程阻塞 IO 缓冲区的非常重要的考虑因素,请参阅此内容。
\n\n如果您希望您的工作在无人值守的情况下运行怎么办?是时候启动 launchd 了,但请记住,整个程序将以 root 身份运行,这与我上面发布的交互式选项不同,其中只有文件操作以 root 身份运行。我编写了这个简单的程序,我想每 5 分钟运行一次。每次运行时,都会向“/Library/Application Support/tempFile”添加一行,其中包含当前日期和时间;每 30 分钟它就会覆盖该文件并重新开始,但只保留最后一个时间戳。这是程序:
\n\npackage maclaunchd;\n\nimport java.io.BufferedWriter;\nimport java.io.File;\nimport java.io.FileWriter;\nimport java.io.IOException;\nimport java.nio.file.Files;\nimport java.nio.file.attribute.FileTime;\nimport java.text.SimpleDateFormat;\nimport java.util.Date;\nimport java.util.logging.Level;\nimport java.util.logging.Logger;\n\npublic class MacLaunchd {\n\n public static void main(String[] args) {\n try {\n SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");\n Date date = new Date();\n File tempFile = new File("/Library/Application Support/tempFile");\n if (!tempFile.exists()) {\n tempFile.createNewFile();\n }\n FileTime creationTime = (FileTime) Files.getAttribute(tempFile.toPath(), "creationTime");\n boolean append = (System.currentTimeMillis() - creationTime.toMillis() <= 1800000);\n FileWriter fw = new FileWriter(tempFile, append);\n try (BufferedWriter bw = new BufferedWriter(fw)) {\n bw.write(sdf.format(date) + "\\n");\n }\n } catch (IOException ex) {\n Logger.getLogger(MacLaunchd.class.getName()).log(Level.SEVERE, null, ex);\n throw new RuntimeException(ex);\n }\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n\n然后我创建了一个名为 \xe2\x80\x9cmaclaunchd.MacLaunchd.daemon.plist\xe2\x80\x9d 的 launchd 作业定义。这里是:
\n\n<?xml version="1.0" encoding="UTF-8"?>\n<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">\n<plist version="1.0">\n<dict>\n <key>GroupName</key>\n <string>wheel</string>\n <key>KeepAlive</key>\n <false/>\n <key>Label</key>\n <string>maclaunchd.MacLaunchd.daemon</string>\n <key>ProgramArguments</key>\n <array>\n <string>/usr/bin/java</string>\n <string>-jar</string>\n <string>/Users/enta/NetBeansProjects/MacLaunchd/dist/MacLaunchd.jar</string>\n </array>\n <key>RunAtLoad</key>\n <false/>\n <key>StandardErrorPath</key>\n <string>/Users/enta/NetBeansProjects/MacLaunchd/dist/err.log</string>\n <key>StandardOutPath</key>\n <string>/Users/enta/NetBeansProjects/MacLaunchd/dist/out.log</string>\n <key>StartInterval</key>\n <integer>300</integer>\n <key>UserName</key>\n <string>root</string>\n <key>WorkingDirectory</key>\n <string>/Users/enta/NetBeansProjects/MacLaunchd/dist/</string>\n</dict>\n</plist>\nRun Code Online (Sandbox Code Playgroud)\n\n接下来,我将文件复制到正确的位置,加载并启动它:
\n\nsudo cp maclaunchd.MacLaunchd.daemon.plist /Library/LaunchDaemons\nsudo launchctl load /Library/LaunchDaemons/maclaunchd.MacLaunchd.daemon.plist\nsudo launchctl start maclaunchd.MacLaunchd.daemon\nRun Code Online (Sandbox Code Playgroud)\n\n如果您输入“/Library/Application Support/tempFile”,您将看到每 5 分钟写入一次日期。要停止、卸载并删除作业运行:
\n\nsudo launchctl stop maclaunchd.MacLaunchd.daemon\nsudo launchctl unload /Library/LaunchDaemons/maclaunchd.MacLaunchd.daemon.plist\nsudo rm /Library/LaunchDaemons/maclaunchd.MacLaunchd.daemon.plist\nRun Code Online (Sandbox Code Playgroud)\n\n现在,您有两个选项可以写入“/Library/Application Support/\xe2\x80\x9d”。
\n| 归档时间: |
|
| 查看次数: |
205 次 |
| 最近记录: |