• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to run macOS command via Java?  RSS feed

 
Marek Curda
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys I am new to JAVA and because I wanted to create something useful for myself I went on and tried to make an app that would put my computer to sleep after certain time.

Now I searched through internet how to run mac OS commands from java and got it partially working... I need to run this in order to put my computer to sleep:



and this is what I got:



where "time" gets me current date in the proper format and "tajm" gets me desired time. And when I run it I get this:


But if I run the code in terminal it works just fine even with the date... also I tried this(just to test if sudo is needed):


but that gives me


I also edited sudoers so pmset doesn't require a password.. so I can run this without a problem


but whenever I try to run the first code it just doesn't go through...
I would appreciate any help. Thank you in advance
 
Campbell Ritchie
Sheriff
Posts: 53532
126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Question too hard for this forum: will be moved elsewhere.

If you need sudo at the terminal, you will doubtless need to include sudo in the commands array. I would suggest you pass that array to Arrays.toString(cmd) and print it out, which will allow you to see whether the format is the same as you are passing to the terminal.
Also, you didn't say whether you are using Runtime#exec to run your program, or something else.
 
Marek Curda
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Welcome to the Ranch

Question too hard for this forum: will be moved elsewhere.

If you need sudo at the terminal, you will doubtless need to include sudo in the commands array. I would suggest you pass that array to Arrays.toString(cmd) and print it out, which will allow you to see whether the format is the same as you are passing to the terminal.
Also, you didn't say whether you are using Runtime#exec to run your program, or something else.


Hi Sorry for posting in beginners, I didnt know where else to put it as I am still a beginner myself

yes I am using to run it, as I didnt find any other way to run macOS commands from java

this is what I get from the array :


Also I am confused as to why did this run without needing sudo but when I type the same thing in terminal it asks for sudo...
 
Knute Snortum
Sheriff
Posts: 3438
85
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cannot run program "/usr/bin/sudo "

You have a space inside the quotes.  Remove it and it should run.  (And remove the other spaces at the end of commands too.)
 
Marek Curda
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:
Cannot run program "/usr/bin/sudo "

You have a space inside the quotes.  Remove it and it should run.  (And remove the other spaces at the end of commands too.)

I thought I had to have them to make spaces between the parameters... after removing the spaces the cannot run program is gone but I get this again:


and this is the array I am using :


Maybe it is impossible to do so I should probably make a timer that would run and after its done it would run this :

Thats the only way I can think of to get it working...
 
Knute Snortum
Sheriff
Posts: 3438
85
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could it be that the command wants a date in DD/MM/YY format?
 
Marek Curda
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:Could it be that the command wants a date in DD/MM/YY format?


When I use the exact same command in terminal it works... But I tried it just to be sure and it gives me same error...
 
Campbell Ritchie
Sheriff
Posts: 53532
126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marek Curda wrote:. . . When I use the exact same command in terminal it works... But I tried it just to be sure and it gives me same error...
Please copy'n'paste the exact command which works. You may be passing date and time as separate arguments. Also have a look at the man pages for pmset and see how many different arguments you can pass.
 
Marek Curda
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Marek Curda wrote:. . . When I use the exact same command in terminal it works... But I tried it just to be sure and it gives me same error...
Please copy'n'paste the exact command which works. You may be passing date and time as separate arguments. Also have a look at the man pages for pmset and see how many different arguments you can pass.


well the command that works is: pmset sleepnow


it also works when I make a String
 
Marek Curda
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry I dont seem to see a button to edit my posts....  man pmset shows examples of using the command and one of them is "pmset schedule wake "07/04/16 20:00:00"" where I just use sleep instead of wake so it should be the same..
 
Campbell Ritchie
Sheriff
Posts: 53532
126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We disabled the edit button ages ago. Does the man say anything about number of arguments? It looks to me that you might need to pass date and time as separate arguments, so try one more element in the command array.
 
Knute Snortum
Sheriff
Posts: 3438
85
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From man pmset:

date/time - "MM/dd/yy HH:mm:ss" (in 24 hour format; must be in quotes)
 
Campbell Ritchie
Sheriff
Posts: 53532
126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
They require the date in month‑day‑year format. That is the problem (I think). When it says "07/04/16...", it is ambiguous; is that 7th April or 4th July?
 
Marek Curda
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:They require the date in month‑day‑year format. That is the problem (I think). When it says "07/04/16...", it is ambiguous; is that 7th April or 4th July?

Well since it asks for  "MM/dd/yy HH:mm:ss" in man pmset one would think that it would work or maybe this code just doesnt return the proper date:


... I tried to split it up but still same error...


 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!