2014-09-19 82 views
0

我正在寫一個類,用於從命令提示符讀取文本後,當我雙擊批處理文件,但當我打開它是提示作爲NoClassDefinition發現錯誤可以任何一個請解決這個問題,我將.class文件放在TCPVisy文件夾中。下面就是我將在這裏如何通過批處理文件執行java

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 

回答

0

你的類TCPVisyMode代碼包下COM(在你的代碼段的第一行聲明。

因此,首先,你的類文件必須是/ com目錄,它位於TCPVisit文件夾下下。

後,你必須恰克你的腳本:

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

注意-cp選項指向TCPVisit文件夾,主類名是完全合格的(全包和類名)。

有關java命令的更多信息here

+0

更改後立即提示並關閉 – Vskiran 2014-09-19 10:31:14

+0

可能是運行時錯誤。嘗試從命令行控制檯運行它並檢查輸出。 – 2014-09-19 10:32:35

+0

我收到此錯誤:無法找到或加載主類TCPServer.TCPVisyMode – Vskiran 2014-09-19 10:35:18