Java中可以通过使用KeychainAccess类来访问Mac OS X钥匙串中的通用密码。以下是一个示例代码: ``` import com.apple.eio.*; public class KeychainExample { public static void main(String[] args) { String serviceName = "MyService"; String accountName = "MyAccount"; String password = "MyPassword"; try { KeychainAccess.setGenericPassword(serviceName, accountName, password.toCharArray()); char[] retrievedPassword = KeychainAccess.getGenericPassword(serviceName, accountName); System.out.println("Retrieved password: " + new String(retrievedPassword)); } catch (Exception e) { e.printStackTrace(); } } } ``` 在上面的示例中,我们使用KeychainAccess类的setGenericPassword方法将密码存储在钥匙串中。然后,我们使用getGenericPassword方法从钥匙串中检索密码。请注意,这些方法可能会抛出异常,因此我们在try-catch块中捕获它们。