Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
网页定时开启器不工作(Java)_Java - Fatal编程技术网

网页定时开启器不工作(Java)

网页定时开启器不工作(Java),java,Java,我正在写一个代码,将在上午8点打开一个网页。我计划在大约早上6:30离开学校之前启动此代码,并阅读在大约下午3:30回家时打开的页面。对于我的代码示例,我将其放入YouTube,以确保我计划打开的网页的完整性。然而,我已经尝试了好几个小时,却没有运气让这段代码正常工作。我对Java语言没有太多的了解,这对我来说是一个巨大的错误,我真的需要尽快启动并运行这段代码。请帮帮我 编辑:我不确定代码的哪一部分失败了。当我在Eclipse控制台中运行它时,它没有显示错误,只是不工作。所以要回答任何类似的问题

我正在写一个代码,将在上午8点打开一个网页。我计划在大约早上6:30离开学校之前启动此代码,并阅读在大约下午3:30回家时打开的页面。对于我的代码示例,我将其放入YouTube,以确保我计划打开的网页的完整性。然而,我已经尝试了好几个小时,却没有运气让这段代码正常工作。我对Java语言没有太多的了解,这对我来说是一个巨大的错误,我真的需要尽快启动并运行这段代码。请帮帮我

编辑:我不确定代码的哪一部分失败了。当我在Eclipse控制台中运行它时,它没有显示错误,只是不工作。所以要回答任何类似的问题,我不确定什么不起作用

import java.awt.Desktop;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;

public class Browser {
    public static void main(String[] args) throws InterruptedException {
        while(true) {
            Timer timer = new Timer();
            TimerTask tt = new TimerTask(){
                public void run(){
                    Calendar cal = Calendar.getInstance(); //this is the method you should use, not the Date(), because it is depreciated.

                    int hour = cal.get(Calendar.HOUR_OF_DAY);//get the hour number of the day, from 0 to 23

                    if(hour == 8){
                        String url = "http://www.youtube.com";

                        if(Desktop.isDesktopSupported()){
                            Desktop desktop = Desktop.getDesktop();
                            try {
                                desktop.browse(new URI(url));
                            } catch (IOException | URISyntaxException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                        } else {
                            Runtime runtime = Runtime.getRuntime();
                            try {
                                runtime.exec("xdg-open " + url);
                            } catch (IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                        }
                    }
                }
            };
            Thread.sleep(1000);
        }
    }
}

您从未计划过TimerTask tt:


你的问题到底是什么?没有让你的代码工作是有点模糊,它的哪一部分是失败的?谢谢你这么多!这使我的代码工作!但在我这样做之后,我只想让它打开一个网页。如果你想在早上8点打开网页一次,那么你应该检查你想要的时间、分钟和秒数。如果不了解您的用例,就很难理解您的需求。我不确定您所说的用例是什么意思。我的意思是,为什么您需要在上午8:00打开一个网站?这可能是一个错误。我正在使用从我的真实网页收集的数据,正如我之前提到的,我在一天中的某个时间(上午8点)为涉及该特定网站的实验保守秘密。
public static void main(String[] args) throws InterruptedException {
    Timer timer = new Timer();
    TimerTask tt = new TimerTask() {
        public void run() {
            Calendar cal = Calendar.getInstance();
            int hour = cal.get(Calendar.HOUR_OF_DAY);
            if(hour == 8) {
                String url = "http://www.youtube.com";
                if(Desktop.isDesktopSupported()){
                    Desktop desktop = Desktop.getDesktop();
                    try {
                        desktop.browse(new URI(url));
                    } catch (IOException | URISyntaxException e) {
                        e.printStackTrace();
                    }
                } else {
                    Runtime runtime = Runtime.getRuntime();
                    try {
                        runtime.exec("xdg-open " + url);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    };
    timer.schedule(tt, 0, 1000);
}