简体   繁体   中英

How to execute a java through batch file

I am writing a class which is used to read a text from command prompt after when i double click on batch file but when i am opening it is prompting as a NoClassDefinition found error can any one please solve this issue i am placing .class file in a TCPVisy folder. Below is the code i am placing here

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();
                }
            }
        }
    }
}

I am placing the command which is in batch file,Inside of TCPVisit folder .class file is located

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

Your class TCPVisyMode is under package com (declared at the very first line of your snippet.

So, first, your class file must be under a /com directory, located under your TCPVisit folder.

After, you have to chage your script to:

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

Note that -cp option is pointing to the TCPVisit folder, and the main class name is fully qualified (full package and class name).

More info about java command here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM