如何在另一个类中调用一个类的main方法? [英] How do I call main method of one class inside another class?

查看:170
本文介绍了如何在另一个类中调用一个类的main方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个线程,当当前应用程序关闭时,该线程必须启动另一个类的main()方法.
我在线程的run()中包含了ClassName.main(someStringArray),但未调用该方法.可能出了什么问题?
我定义的线程:

I have a thread that, when the current application closes, must start the main() method of another class.
I included ClassName.main(someStringArray) in the run() of the thread but the method wasn't called. What might have gone wrong?
The Thread I defined:

private class VideoCreator extends Thread{
        public VideoCreator(){
            pathToPass = savePath + "/" + "video.mov";
            passVect.add("-w");
            passVect.add("1280");
            passVect.add("-h");
            passVect.add("800");
            passVect.add("-f");
            passVect.add("25");
            passVect.add("-o");
            passVect.add(pathToPass);
        }
        @Override
        public void run(){
            try{
                jpegFiles = Files.newDirectoryStream(Paths.get(pathToPass).getParent(),"*.jpg");
                for(Path jpegFile : jpegFiles){
                    passVect.add(jpegFile.toString());
                }
            }catch(IOException e){

            }
            try{
                JpegImagesToMovie.main((String[])passVect.toArray());
            }catch(Exception e){
                System.out.println("Dammit Error!");
                e.printStackTrace();
            }
        }
        public void cleanUp(){

        }
        String pathToPass;
        Vector<String> passVect = new Vector<>(100,200);
        DirectoryStream<Path> jpegFiles;
    }

推荐答案

代替

(String[])passVect.toArray()

您应该写

passVect.toArray(new String[passVect.size()])

或(性能更短但效果更差)

or (shorter but less performant)

passVect.toArray(new String[0])

原因是 toArray() 将始终返回一个Object[]数组,即使其所有成员都是字符串,也无法将其转换为String[]数组. (顺便说一句,可能是相反的:您可以在期望Object[]的地方传递String[]

The reason is that toArray() will always return an Object[] array, which you cannot convert to a String[] array even if all its members are strings. (The reverse, by the way, is possible: you can pass a String[] in places expecting Object[], which is used by various methods of the Arrays class. In fact, the thing returned from the toArray() method might have been a String[], even if in a standards-compliant implementation it is not. This is the reason why the compiler didn't complain: it doesn't know or care about the internals of the method, and judging from the return type, an explicit cast to an array of a subclass might be possible if the array was created as such.)

toArray(T[]) 调用将返回所需类型的数组.如果传递的参数长度正确,则将直接使用它;否则,将使用0.否则将分配一个新的数组.因此,首先分配正确的长度将避免沿途进行一次分配.

The toArray(T[]) call returns an array of the required type, if you pass an array of that type as an argument. If the passed argument has the correct length, it will be used directly; otherwise a new array will be allocated. For this reason, allocating the correct length in the first place will avoid one allocation along the way.

这篇关于如何在另一个类中调用一个类的main方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆