3

I'm trying to make a simple java program to unhide the ~\Library\ folder on osx using terminal commands. As far as I have researched the code to run system commands from java is Runtime.getRuntime().exec(); and is listed as such in every place I look it up.

However, my program doesn't work. Main method below.

public static void main(String[] args) throws IOException {

    String[] noHide = {"chflags"," " ,"nohidden"," ", "~/Library/"};
    try {
        Runtime.getRuntime().exec(noHide);
        System.out.println("library unhidden");
    } catch (Exception e ) {
        e.printStackTrace();
    }
}

This program throws no exception, and compiles and executes fine, but the Library folder simply won't unhide. No matter what I reformat the cmd String. None of the formats below work

String noHide = "chflags nohidden ~/Library";
String[] noHide = {"chflags", "nohidden","~/Library"};
String[] noHide = {"chflags"," " ,"nohidden"," ", "~/Library/"};

If I remove the spaces they throw exceptions (well, not the String array objects). I can run the command (chflags noHidden ~/Library) absolutely fine from the osx terminal. Anyone have an idea why?

1 Answer 1

2

You need to use a try and catch, which you have. But, your main should be like this:

public static void main(String[] args) {
    String[] noHide = {"chflags", "nohidden","~/Library"};
    try {
        Runtime.getRuntime().exec(noHide);
    } 
    catch (Exception e) {
        }
    }

Basically, you don't need throws IOException. This worked for me, so if it still isn't working in your program, there may be a bigger problem with the way you have something set up.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.