我的项目有两个程序。我想通过 program2 作为线程运行 program1。我尝试在 program1 中扩展 Thread 类,但是我遇到了很多错误:
public void main(String[] args) {
Void is an invalid type for the variable main.
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/