java - 无法编译嵌套方法

标签 java multithreading

我的项目有两个程序。我想通过 program2 作为线程运行 program1。我尝试在 program1 中扩展 Thread 类,但是我遇到了很多错误:

  1. public void main(String[] args) {

Void is an invalid type for the variable main.

  1. private static void start(Result result) {

Void is an invalid type for the variable start.

程序 1:

public class HelloWorld extends Thread {

    private String[] args;         

    public HelloWorld(String[] args){
        this.args = args;
    }

        int i=1;
        String resultText;
            try {
                URL url;
                if (args.length > 0) {
                    url = new File(args[0]).toURI().toURL();
                }
                else {
                    url = HelloWorld.class.getResource("helloworld.config.xml");
                }

                ConfigurationManager cm = new ConfigurationManager(url);
                Recognizer recognizer = (Recognizer) cm.lookup("recognizer");
                Microphone microphone = (Microphone) cm.lookup("microphone");
                recognizer.allocate();
                if (microphone.startRecording()) {
                    while (true) {
                        System.out.println("Start speaking. Press Ctrl-C to quit.\n");           
                            Result result = recognizer.recognize();
                    }
                } 
                else {
                    System.out.println("Cannot start microphone.");
                    recognizer.deallocate();
                    System.exit(1);
                }

            }
            catch (IOException e) {
                System.err.println("Problem when loading HelloWorld: " + e);
                e.printStackTrace();
            }
        }

        private static void start_recognition(Result result) {
            if (result != null)
            {
                resultText = result.getBestFinalResultNoFiller();
                System.out.println("You said: " + resultText + "\n");
                if(resultText.equalsIgnoreCase("Command Prompt"))
                {
                    try{
                    Runtime.getRuntime().exec("cmd /c start cmd");
                    }
                    catch(Exception er){    
                    }
                }

            }

        }
    }
}

程序 2:

public class App {
    public static void main(String[] args) {
        HelloWorld obj = new HelloWorld(args);
        obj.start();
    }
}

如何通过 program2 将 program1 作为线程运行?

根据建议更新代码:

public class HelloWorld extends Thread{
    public void run() {
        int i=1;
        String resultText;

            try {
                URL url;
                if (args.length > 0) { // Getting error in this line

args cannot be resolved to a variable.

                    url = new File(args[0]).toURI().toURL(); // And the same error in this line
                }
                else {
                    url = HelloWorld.class.getResource("helloworld.config.xml");
                }

                ConfigurationManager cm = new ConfigurationManager(url);
                Recognizer recognizer = (Recognizer) cm.lookup("recognizer");
                Microphone microphone = (Microphone) cm.lookup("microphone");
                recognizer.allocate();
                if (microphone.startRecording()) {
                    while (true) {
                        System.out.println("Start speaking. Press Ctrl-C to quit.\n");           
                            Result result = recognizer.recognize();
                            start_recognition(result);
                    }
                } 
                else {
                    System.out.println("Cannot start microphone.");
                    recognizer.deallocate();
                    System.exit(1);
                }

            }
            catch (IOException e) {
                System.err.println("Problem when loading HelloWorld: " + e);
                e.printStackTrace();
            }
    }

    private void start_recognition(Result result) {
        {
            if (result != null)
            {
                String resultText = result.getBestFinalResultNoFiller();
                System.out.println("You said: " + resultText + "\n");
                if(resultText.equalsIgnoreCase("Command Prompt"))
                {
                    try{
                    Runtime.getRuntime().exec("cmd /c start cmd");
                    }
                    catch(Exception er){    
                    }
                }

            }

        }

    }
}

最佳答案

在你的 HelloWorld 类中,去掉 public void main(String[] args)

它应该是这样的:

public class HelloWorld extends Thread {
public void run() {
    int i=1;
    String resultText;
        try {
            URL url;
            if (args.length > 0) {
                url = new File(args[0]).toURI().toURL();
            }
            else {
                url = HelloWorld.class.getResource("helloworld.config.xml");
            }

更多信息,请引用此链接:Java Class

关于java - 无法编译嵌套方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29002926/

相关文章:

c - pclose() 在多线程环境中过早返回 (Solaris 11)

java - 如果未收到确认,如何设计一个发送记录并重试发送记录的系统?

android - 将 AsyncTask 与数据库一起使用

java - 是什么导致了这个与类加载器和文件相关的 NullPointerException?

java - 基本的maven问题: Does maven transitively install dependencies?

c++ - C++使用默认参数制作新的线程函数

java - 使用多核的前缀搜索算法

Java 解析器测试

java - .classpath 中导出的属性有什么作用?

java - 如何合并两个并行执行的两个 react 器通量,返回列表并合并结果