如何在eclipse中解决java.lang.ClassNotFoundException

时间:2017-06-30 16:01:03

标签: java eclipse jar executable-jar exe4j

我尝试从我的代码创建exe文件,但它让我犯了错误

java.lang.ClassNotFoundException: Access2
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)

我使用exe4j工具从jar文件生成exe 我用了Jdk 1.8.0_25

这是我的主要课程(Access2)的一部分

  public class Access2
{
  BufferedReader in;
  Writer out;
  String CommData = "";
  String TextData = "";
  int FrameNo = 1;
  String STX = String.valueOf('\002');
  String ETX = String.valueOf('\003');
  String EOT = String.valueOf('\004');
  String ENQ = String.valueOf('\005');
  String ACK = String.valueOf('\006');
  String LF = String.valueOf('\n');
  String CR = String.valueOf('\r');
  String DEL = String.valueOf('\007');
  String NAK = String.valueOf('\025');
  String SYN = String.valueOf('\026');
  String ETB = String.valueOf('\027');
  String CRLF = this.CR + this.LF;
  String enc;
  String LastType = "";
  String PatientID;
  String PatientName;
  String SampleID;
  String Result = "";
  String Parameter = "";
  String PatientComment;
  String SampleComment;
  String ResultComment = "";
  String QuerySampleID;
  boolean QueryAsked = false;
  Connection connection = null;
  Statement stmt = null;
  PreparedStatement prepStatement = null;
  ResultSet rs = null;
  String machine = Setting.GetPropVal("MachineName");
  ArrayList<String> DataToSend = new ArrayList();
  int Max_Farme_Size = 240;
  int FIndex = 1;
  int CurIndex = 0;
  int RowIndex = 1;
  String Frame_Sep;
  int retries = 6;
  Connection connection2 = null;
  Statement stmt2 = null;
  PreparedStatement prepStatement2 = null;
  ResultSet rs2 = null;

  public Access2(Socket s)
  {
    try
    {
      this.enc = Setting.GetPropVal("Encoding");
      this.in = new BufferedReader(new InputStreamReader(s.getInputStream(), this.enc));
      this.out = new BufferedWriter(new OutputStreamWriter(s.getOutputStream(), this.enc));
    }
    catch (IOException e)
    {
      JOptionPane.showMessageDialog(null, e, "IOException", 1);
      System.exit(0);
    }
    catch (Exception e)
    {
      JOptionPane.showMessageDialog(null, e, "Exception", 1);
      System.exit(0);
    }
  }

  public Access2(SerialPort ser)
  {
    try
    {
      this.enc = Setting.GetPropVal("Encoding");
      this.in = new BufferedReader(new InputStreamReader(ser.getInputStream(), this.enc));
      this.out = new BufferedWriter(new OutputStreamWriter(ser.getOutputStream(), this.enc));
    }
    catch (IOException e)
    {
      JOptionPane.showMessageDialog(null, e, "IOException", 1);
      System.exit(0);
    }
    catch (Exception e)
    {
      JOptionPane.showMessageDialog(null, e, "Exception", 1);
      System.exit(0);
    }
  }

  public void NextFrameNo()
  {
    this.FrameNo += 1;
    if (this.FrameNo > 7) {
      this.FrameNo = 0;
    }
  }

  public void SaveData(String data)
  {
    String data2save = data.replaceAll(this.ENQ, "<ENQ>").replaceAll(this.STX, "<STX>").replaceAll(this.ETX, "<ETX>").replaceAll(this.EOT, "<EOT>").replaceAll(this.CR, "<CR>").replaceAll(this.CRLF, "<CRLF>").replaceAll(this.ETB, "<ETB>");

    LogFile.createLog(this.machine + "_" + CurrentDate.getDate(2) + ".txt", "\r\n[" + CurrentDate.getDate(1) + "]" + ":" + data2save);
  }

  public String[] ParseFrames(String Data)
  {
    String[] Frame = Data.split(this.CR);
    return Frame;
  }

  public boolean CheckSumCorrect()
  {
    String Frame = GetFrameData(this.CommData, 1);
    String CalculatedSum = GetCheckSum(Frame);
    String FrameSum = GetFrameData(this.CommData, 3);
    boolean Status = CalculatedSum.equals(FrameSum);
    return Status;
  }

  public boolean CheckFarmeNum()
  {
    String FN = GetFrameData(this.CommData, 4);
    boolean Status;
    if (Integer.toString(this.FrameNo).equals(FN))
    {
      Status = true;
      NextFrameNo();
    }
    else
    {
      Status = false;
    }
    return Status;
  }

  public String GetCheckSum(String Dat)
  {
    int x = 0;
    for (int i = 0; i < Dat.length(); i++) {
      x += Dat.charAt(i);
    }
    String S = "00" + Integer.toHexString(x % 256);
    String CheckSum = S.substring(S.length() - 2).toUpperCase();
    return CheckSum;
  }

  public String GetFrameData(String Data, int FrameType)
  {
    String Frame = "";
    int pos1 = Data.indexOf(this.STX);
    int pos2 = Data.indexOf(this.ETX);
    int pos3 = Data.indexOf(this.ETB);
    if (FrameType == 1)
    {
      if (Data.contains(this.ETX)) {
        Frame = Data.substring(pos1 + 1, pos2 + 1);
      } else if (Data.contains(this.ETB)) {
        Frame = Data.substring(pos1 + 1, pos3 + 1);
      }
    }
    else if (FrameType == 2)
    {
      if (Data.contains(this.ETX)) {
        Frame = Data.substring(pos1 + 2, pos2);
      } else if (Data.contains(this.ETB)) {
        Frame = Data.substring(pos1 + 2, pos3);
      }
    }
    else if (FrameType == 3)
    {
      if (Data.contains(this.ETX)) {
        Frame = Data.substring(pos2 + 1, pos2 + 3);
      } else if (Data.contains(this.ETB)) {
        Frame = Data.substring(pos3 + 1, pos3 + 3);
      }
    }
    else if (FrameType == 4) {
      Frame = Data.substring(pos1 + 1, pos1 + 2);
    }
    return Frame;
  }

  public void sendtoport(String Sample_ID)
  {
    try
    {
      int sent = 0;
      for (String DataToSend1 : this.DataToSend)
      {
        this.out.write(DataToSend1);
        this.out.flush();
        SaveData(DataToSend1);
        char c = (char)this.in.read();
        String control = String.valueOf(c);
        if (control.equals(this.ACK))
        {
          sent = 1;
        }
        else if (control.equals(this.NAK))
        {
          for (int x = 1; x <= this.retries; x++)
          {
            this.out.write(DataToSend1);
            this.out.flush();
            SaveData(DataToSend1);
          }
          sent = 0;
          break;
        }
      }
      this.DataToSend.clear();
      this.FIndex = 1;
      this.out.write(this.EOT);
      this.out.flush();
      SaveData(this.EOT);
      try
      {
        this.connection2 = DBconnection.getConnection();
        String ExecProc = "HK_Update_SCH ?,?,?";
        this.connection2.setAutoCommit(false);
        this.prepStatement = this.connection2.prepareStatement(ExecProc);
        this.prepStatement2.setString(1, Sample_ID);
        this.prepStatement2.setString(2, "Access2");
        this.prepStatement2.setString(3, "");
        this.prepStatement2.executeUpdate();
        this.connection2.commit();
      }
      catch (SQLException e)
      {
        JOptionPane.showMessageDialog(null, e, "SQLException", 1);
      }
      catch (Exception e)
      {
        JOptionPane.showMessageDialog(null, e, "Exception", 1);
      }
      Thread.sleep(10L);
    }
    catch (IOException e)
    {
      JOptionPane.showMessageDialog(null, e, "IOException", 1);
      System.exit(0);
    }
    catch (HeadlessException|InterruptedException e)
    {
      JOptionPane.showMessageDialog(null, e, "Exception", 1);
      System.exit(0);
    }
  }

  public void SplitFrames(String Frame)
  {
    int NumSplit = Frame.length() / this.Max_Farme_Size;
    for (int i = 0; i <= NumSplit; i++) {
      if (i < NumSplit)
      {
        String part = Frame.substring(i * this.Max_Farme_Size, i * this.Max_Farme_Size + this.Max_Farme_Size);
        String IntermdiateFrame = this.STX + this.FIndex + part + this.ETB + GetCheckSum(new StringBuilder().append(this.FIndex).append(part).append(this.ETB).toString()) + this.CRLF;
        this.DataToSend.add(this.DataToSend.size(), IntermdiateFrame);
        NextFIndexNo();
      }
      else
      {
        String part = Frame.substring(i * this.Max_Farme_Size);
        String LastFrame = this.STX + this.FIndex + part + this.ETX + GetCheckSum(new StringBuilder().append(this.FIndex).append(part).append(this.ETX).toString()) + this.CRLF;
        this.DataToSend.add(this.DataToSend.size(), LastFrame);
        NextFIndexNo();
      }
    }
  }

  public void NextFIndexNo()
  {
    this.FIndex += 1;
    if (this.FIndex > 7) {
      this.FIndex = 0;
    }
  }

  public void SendOrder(String[] OrderFrame)
  {
    this.DataToSend.clear();
    this.DataToSend.add(0, this.ENQ);
    for (int i = 0; i < OrderFrame.length; i++) {
      if (OrderFrame[i].length() > this.Max_Farme_Size)
      {
        SplitFrames(OrderFrame[i]);
      }
      else
      {
        String SingleFrame = this.STX + this.FIndex + OrderFrame[i] + this.ETX + GetCheckSum(new StringBuilder().append(this.FIndex).append(OrderFrame[i]).append(this.ETX).toString()) + this.CRLF;
        this.DataToSend.add(this.DataToSend.size(), SingleFrame);
        NextFIndexNo();
      }
    }
  }

  public void SendOrderData(String SampleData, String TestCodes)
  {
    HeaderRecord HRec = new HeaderRecord();
    PatientRecord PRec = new PatientRecord();
    OrderRecord ORec = new OrderRecord();
    MessageTerminatorRecord LRec = new MessageTerminatorRecord();

    PRec.SetPropertyValue("PracticePatientID", SampleData);
    PRec.SetPropertyValue("LabPatientID", SampleData);
    PRec.SetPropertyValue("PatientSex", "M");

    ORec.SetPropertyValue("SampleID", SampleData);
    ORec.SetPropertyValue("ActionCode", "N");

    ORec.SetPropertyValue("UniversalTestID", "^^^" + TestCodes.replace(",", "\\^^^"));
    ORec.SetPropertyValue("SampleType", "Serum");
    ORec.SetPropertyValue("ReportTypes", "O");

    String[] records = new String[4];
    records[0] = HRec.FormatFrame();
    records[1] = PRec.FormatFrame();
    records[2] = ORec.FormatFrame();
    records[3] = LRec.FormatFrame();

    String Msg = HRec.FormatFrame() + PRec.FormatFrame() + ORec.FormatFrame() + LRec.FormatFrame();
    System.out.println(">>>>>>>>" + Msg);
    try
    {
      SendOrder(records);
    }
    catch (Exception e)
    {
      JOptionPane.showMessageDialog(null, e, "Exception", 1);
      System.exit(0);
    }
  }

  public void FramesReady(String[] Frame)
  {

  }

  public void ManageData()
  {
    try
    {
      if (this.CommData.contains(this.ENQ))
      {
        SaveData(this.CommData);
        this.CommData = "";
        this.out.write(this.ACK);
        this.out.flush();
      }
      else if (this.CommData.contains(this.CRLF))
      {
        SaveData(this.CommData);
        if ((CheckSumCorrect()) && (CheckFarmeNum()))
        {
          this.TextData += GetFrameData(this.CommData, 2);
          this.CommData = "";
          this.out.write(this.ACK);
          this.out.flush();
        }
        else if ((!CheckSumCorrect()) || (!CheckFarmeNum()))
        {
          this.CommData = "";
          this.out.write(this.NAK);
          this.out.flush();
        }
      }
      else if (this.CommData.contains(this.EOT))
      {
        SaveData(this.CommData);
        String[] x = ParseFrames(this.TextData);
        FramesReady(x);
        this.TextData = "";
        this.FrameNo = 1;
        this.CommData = "";
      }
    }
    catch (IOException e)
    {
      JOptionPane.showMessageDialog(null, e, "IOException", 1);
      System.exit(0);
    }
    catch (Exception e)
    {
      JOptionPane.showMessageDialog(null, e, "Exception", 1);
      System.exit(0);
    }
  }

  public void Server()
  {
    try
    {
      for (;;)
      {
        char c = (char)this.in.read();
        System.out.print(c);
        this.CommData += c;
        ManageData();
      }
    }
    catch (IOException e)
    {
      JOptionPane.showMessageDialog(null, e, "IOException", 1);
      System.exit(0);
    }
    catch (Exception e)
    {
      JOptionPane.showMessageDialog(null, e, "Exception", 1);
      System.exit(0);
    }
  }

  public static void main(String[] args)
    throws Exception
  {
    switch (Setting.GetPropVal("Socket"))
    {
    case "TCPIP": 
      String IP = Setting.GetPropVal("IPaddress");

      int Port2Rec = Integer.parseInt(Setting.GetPropVal("INport"));
      InetAddress addr = InetAddress.getByName(IP);
      String host = addr.getHostName();
      Socket s = new Socket(host, Port2Rec);
      System.out.println("Connected to :" + host + "/" + Port2Rec);
      Access2 TcpChat = new Access2(s);
      TcpChat.Server();

      break;
    case "Serial": 
      SerialPort serialPort = null;

      Enumeration portList = CommPortIdentifier.getPortIdentifiers();
      while (portList.hasMoreElements())
      {
        CommPortIdentifier portId = (CommPortIdentifier)portList.nextElement();
        if ((portId.getPortType() == 1) && 

          (portId.getName().equals(Setting.GetPropVal("ComPort"))))
        {
          serialPort = (SerialPort)portId.open("HPS", 10);
          serialPort.setSerialPortParams(9600, 8, 1, 0);
        }
      }
      System.out.println("Connected to :" + serialPort.getName());
      Access2 SerialChat = new Access2(serialPort);
      SerialChat.Server();
    }
  }
}

当我从eclipse运行程序时 显示“错误的路径或文件丢失”

public class Setting
{
  public static Properties props;
  public static String propVal;

  public static String GetPropVal(String param)
  {
    try
    {
      props = new Properties();
      props.load(new FileInputStream("Access2.ini"));
      propVal = props.getProperty(param);
    }
    catch (IOException e)
    {
      JOptionPane.showMessageDialog(null, "Wrong Path OR file Lost", "Alert", 1);
      System.exit(0);
    }
    return propVal;
  }
}

我该如何解决?它说我的班级没找到,但它存在什么解决方案?

1 个答案:

答案 0 :(得分:1)

这是因为类文件的包装不当,或者是包内不正确的Java文件编译。

使用包含Java文件的干净目录来获取Maven等构建工具,然后生成.jar。

基本上,如果可以的话,创建一个新的Maven或Ant项目并创建具有相同名称的Java文件,并将内容逐个粘贴到新文件中。

编辑:确保主包中只有一个公共类。