如何通过批处理文件执行java

时间:2014-09-19 10:21:29

标签: java batch-file bufferedreader

我正在编写一个用于从命令提示符中读取文本的类,当我双击批处理文件但是当我打开它时,提示作为NoClassDefinition发现错误,任何人都可以解决我正在放置的这个问题。 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

1 个答案:

答案 0 :(得分:0)

您的类TCPVisyMode位于包com下(在您的代码段的第一行声明。

因此,首先,您的类文件必须位于TCPVisit文件夹下的/ com目录下。

之后,你必须将你的脚本变为:

java -Xms512m -Xmx1024m -Dsun.lang.ClassLoader.allowArraySyntax=true -cp ./TCPVisit com.TCPVisyMode
pause

请注意,-cp选项指向TCPVisit文件夹,主类名称是完全限定的(完整包和类名)。

有关java command here的更多信息。