Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/381.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何解决eclipse中的java.lang.ClassNotFoundException_Java_Eclipse_Jar_Executable Jar_Exe4j - Fatal编程技术网

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

如何解决eclipse中的java.lang.ClassNotFoundException,java,eclipse,jar,executable-jar,exe4j,Java,Eclipse,Jar,Executable Jar,Exe4j,我试图用我的代码创建exe文件,但出现了get me错误 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)

我试图用我的代码创建exe文件,但出现了get me错误

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 我使用了JDK1.8.025

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


我怎样才能解决这个问题?它说找不到我的类,但它存在解决方案是什么?

这是因为类文件的包装不当,或者更确切地说,在包中对Java文件的编译不当

使用一个包含Java文件的干净目录的构建工具,比如Maven,然后生成一个.jar

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


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

异常和错误是不同的,这有助于说明找不到哪个类。那么我该怎么办?它说找不到我的类,但它存在解决方案是什么?
Access2 TcpChat=new Access2(s)那是什么?你是如何在没有构造函数的情况下实例化一个主类的????这就是你提到创建jar时的全部代码吗?你确定它指向了正确的主类吗?你还没有告诉我们找不到哪个类。对不起,我是初学者,你能提供我的教程吗?好吧,忘了构建工具,如果没有它们,它可能会工作,除非你有一些外部依赖关系。在eclipse中创建一个简单的Java项目,将Java文件复制到src文件夹中,然后右键单击project并选择“导出”,然后选择“src”并单击Finish。是的,是的。。然后我生成jar文件,并使用exe4j生成exe文件,但并没有找到主类。是否有多个公共类?
  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();
    }
  }
}
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;
  }
}