[英]How to execute a java through batch file
我正在編寫一個用於在雙擊批處理文件后從命令提示符中讀取文本的類,但是當我打開它時,由於NoClassDefinition發現錯誤提示任何人都可以解決此問題,我將.class文件放入一個TCPVisy文件夾。 下面是我在這里放置的代碼
package com;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
public class TCPVisyMode {
public static String ip;
public static String port;
public TCPVisyMode() {
}
public static void main(String[] args) throws IOException {
TCPVisyMode visyMode = new TCPVisyMode();
DataInputStream dis
= new DataInputStream(visyMode.getClass().getResourceAsStream("CTASSimulator.config"));
String strLine;
while ((strLine = dis.readLine()) != null) {
String[] keyValues = strLine.split("=");
if (keyValues[0].equalsIgnoreCase("EQPTINFCLIENTIP")) {
ip = keyValues[1];
System.out.println("IP is " + ip);
} else if (keyValues[0].equalsIgnoreCase("EQPTINFSERVERPORT")) {
port = keyValues[1];
System.out.println("Port is " + port);
}
}
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Socket clientsoc = null;
BufferedReader responsereader = null;
DataOutputStream serverstream = null;
VisyReader reader = null;
String tempchar = "";
String inputstring = "";
while (true) {
try {
if (clientsoc == null) {
clientsoc = new Socket(ip, Integer.parseInt(port));
//clientsoc = new Socket(ip,port);
responsereader
= new BufferedReader(new InputStreamReader(clientsoc.getInputStream()));
serverstream = new DataOutputStream(clientsoc.getOutputStream());
reader = new VisyReader(responsereader);
reader.setName("VisyReader");
reader.start();
}
if (clientsoc != null) {
tempchar = br.readLine();
inputstring = inputstring + tempchar;
if (inputstring.startsWith("exit>>>")) {
serverstream.close();
br.close();
clientsoc.close();
break;
}
if (inputstring.endsWith(">>>")) {
inputstring = inputstring.replaceAll(">>>", "");
serverstream.write(new byte[]{31, 65});
serverstream.write(inputstring.getBytes());
serverstream.write(new byte[]{-1});
serverstream.flush();
inputstring = "";
}
} else {
try {
java.lang.Thread.sleep(2000);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
try {
serverstream.close();
serverstream = null;
clientsoc.close();
clientsoc = null;
} catch (Exception e1) {
// TODO Auto-generated catch block
// LogWriter.LogErrorMessage(e1);
clientsoc = null;
serverstream = null;
}
try {
java.lang.Thread.sleep(2000);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}
}
我將命令放在批處理文件中,位於TCPVisit文件夾.class文件的內部
java -Xms512m -Xmx1024m -Dsun.lang.ClassLoader.allowArraySyntax=true -cp . TCPVisit/TCPVisyMode
pause
您的類TCPVisyMode在com包下(在代碼段的第一行中聲明。
因此,首先,您的類文件必須位於TCPVisit文件夾下的/ com目錄下。
之后,您必須修改腳本以:
java -Xms512m -Xmx1024m -Dsun.lang.ClassLoader.allowArraySyntax=true -cp ./TCPVisit com.TCPVisyMode
pause
請注意,-cp選項指向TCPVisit文件夾,並且主類名是完全限定的(完整的包和類名)。
有關Java命令的更多信息,請參見此處 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.