我的项目有两个程序。我想通过程序 2 作为线程运行程序 1。我尝试在程序 1 中扩展 Thread 类,但是,我收到了很多错误:
-
public void main(String[] args) {
Void 是变量 main 的无效类型。
-
private static void start(Result result) {
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();
}
}
如何通过程序 2 将程序 1 作为线程运行?
建议后更新代码:
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
参数不能解析为变量。
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, 摆脱公共 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 类