0

So I am making a program using Java that is going to close out of applications at certain times and allow them at certain times. For example, a game is only allowed from 5PM to 7PM. But I don't know how to make it so that it closes at the specific time and you can only open it at certain times. I have the rest of the code but not the permissions for the times. Your help is appreciated thanks,

Chris Martin
  • 28,558
  • 6
  • 66
  • 126
  • In the future, try to find a more descriptive title for your questions. "Need help making a Java program" describes almost every question tagged [java], and so is rather unhelpful when browsing the list of questions. – Jeffrey Bosboom Sep 29 '14 at 04:08
  • Trying to riddle out a new title but can't – Pedantic Sep 29 '14 at 04:23
  • There seem to be two very different questions here - How to enforce the security policy (which is going to be difficult/impossible and platform-specific) and how to do things based on time of day. Which aspect are you asking about? – Chris Martin Sep 29 '14 at 05:49
  • Okay sorry about that. – Nick Cottam Sep 29 '14 at 13:57
  • I'm trying to do both and the security policy is a lot harder than it seems. So if you right click a program or folder on your system and go to properties there is a security tab (depending on your permissions) and you can deny access to specific folder or program and i'm trying to make it so than it does that where you can't access it but I want to be able to access it at certain times and not have to go into properties every time and change it for every person. So is there a way where you can do something like that and keep the time permissions in place so it opens and closes at the times? – Nick Cottam Sep 29 '14 at 14:15

2 Answers2

0

You can use TimerTask for this purpose.

Example:

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

public class ExitOn {

    Timer timer = new Timer();
    TimerTask exitApp = new TimerTask() {
        @Override
        public void run() {
            System.exit(0);
        }
    };

    public ExitOn() {
    timer.schedule(exitApp, new Date(System.currentTimeMillis()+5*1000));//Exits after 5sec of starting the app
    while(true)
        System.out.println("hello");
    }

    public static void main(String[] args) {
        new ExitOn();
    }

}

Sample Code found here

StackFlowed
  • 6,575
  • 1
  • 26
  • 41
0
import java.util.Scanner;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;



public class Test {
    private static Scanner scanner;

    public static void main(String[] args) throws Exception {
        ScheduledExecutorService ex = Executors.newScheduledThreadPool(2);
        ex.scheduleAtFixedRate(new Start(),  0 , 24, TimeUnit.HOURS);
        ex.scheduleAtFixedRate(new End(),  2 , 24, TimeUnit.HOURS);
        scanner = new Scanner(System.in);
        while(true){
            if(scanner.hasNext() && "Q".equals(scanner.next())){
                System.exit(0);
            }
            Thread.sleep(10000);
        }
    }
}

class Start implements Runnable{

    @Override
    public void run() {
        //start your game
    }

}

class End implements Runnable{

    @Override
    public void run() {
        //shutdown your game
    }

}

As jwenting mentioned, you also want to kill or start a program or just change the permission of a game.

Here is the link to kill a process in windows. Hope it can help. How to find and kill running Win-Processes from within Java?

To avoid a game being started, I have no idea yet.


The java 7 provide new functions to change the permissions of a file. Could this can help you set the permission of a file in special time.

Path file = ...;  
Set<PosixFilePermission> perms = PosixFilePermissions.fromString("rw-------");  
FileAttribute<Set<PosixFilePermission>> attr = PosixFilePermissions.asFileAttribute(perms);  
try {  
    Attributes.setPosixFilePermissions(file, perms);  
} catch (IOException x) {  
    System.err.println(x);  
}  
Community
  • 1
  • 1
paco alcacer
  • 181
  • 1
  • 10
  • That won't stop the user running something outside of this program, is therefore useless for OP. – jwenting Sep 29 '14 at 06:55
  • The question is not very clear. I have the rest of the code seems to mean he can insert his code into mine to meet his requirement. There are multi-ways to change the permissions of a Game in different platform with different strategy. – paco alcacer Sep 29 '14 at 07:18
  • no, he wants a Java program that stops specific other programs from being launched by the operating system at specific times. Not going to work. – jwenting Sep 29 '14 at 09:17
  • For example, a win-process can be killed inside a java function as http://stackoverflow.com/questions/81902/how-to-find-and-kill-running-win-processes-from-within-java . The questioner just need put the code in the linked answer to mime, isn't it? – paco alcacer Sep 29 '14 at 09:59
  • So I am also trying to make it run in the background so than no one will see it and it stays running if it becomes an auto run program. – Nick Cottam Sep 29 '14 at 14:18