无法编译嵌套方法

时间:2015-03-12 06:07:59

标签: java multithreading

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

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

    Void是变量main的无效类型。

    1. private static void start(Result result) {
    2.   

      Void是变量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无法解析为变量。

                          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){    
                          }
                      }
      
                  }
      
              }
      
          }
      }
      

1 个答案:

答案 0 :(得分:3)

在你的班级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