简体   繁体   English

需要帮助从头开始设置具有多个 .java 文件的 intellij java 项目

[英]Need help setting up intellij java project with multiple .java files from scratch

Edited to restart question from scratch due to complaints.由于投诉,编辑从头开始重新启动问题。 I am a newbie to this format and to intellij so please excuse...我是这种格式和 intellij 的新手,所以请原谅......

I am building a project in intellij for class.我正在 intellij 中为课堂构建一个项目。 This project imports jnetcap and uses it to process a captured pcap file.该项目导入 jnetcap 并使用它来处理捕获的 pcap 文件。 My issue is I have two class files I am trying to integrate.我的问题是我有两个要集成的类文件。 NetTraffic which is the user interface class, and ProcessPacket that actually reads in the packet and does the work. NetTraffic 是用户界面类,ProcessPacket 实际读取数据包并完成工作。

I have tried to make a project and import ProcessPacket into NetPacket but have been unsuccessful so far.我曾尝试制作一个项目并将 ProcessPacket 导入 NetPacket,但到目前为止没有成功。 I am sure I am missing something simple in this process but I just can not find anything showing the proper way to do this.我确信我在这个过程中遗漏了一些简单的东西,但我找不到任何显示正确方法的东西。

I have gotten it working by making a package under the src directory and adding both files to that package.我通过在 src 目录下创建一个包并将这两个文件添加到该包中来使其工作。 This doesn't require an import from the NetPacket class and seems to work but my worry is that I need to be able to run this from a linux command line.这不需要从 NetPacket 类导入并且似乎可以工作,但我担心的是我需要能够从 linux 命令行运行它。 I have been working all semester so far with everything in one source file so it hasn't been an issue until now.到目前为止,我整个学期都在使用一个源文件中的所有内容,所以直到现在这都不是问题。 I don't remember using packages in the past under eclipse to do this.我不记得过去在 eclipse 下使用包来做到这一点。

Can someone offer a step by step process on how to properly add these source files to my project so that I am able to import ProcessPacket into NetTraffic or will leaving like this in a package work fine?有人可以提供有关如何将这些源文件正确添加到我的项目中的分步过程,以便我能够将 ProcessPacket 导入到 NetTraffic 中,还是将这样留在包中可以正常工作?

The files in question reside in package named nettraffic in src directory.有问题的文件位于 src 目录中名为 nettraffic 的包中。

NetTraffic.java网络流量.java

 package nettraffic;

public class NetTraffic {
    public static ProcessPacket pp;
    public static void main (String args[]) {
        pp = new ProcessPacket();
        pp.PrintOut();
    }

}

ProcessPacket.java进程包

package nettraffic;
import org.jnetpcap.*;
public class ProcessPacket {

    public ProcessPacket() {
    }
    public void PrintOut() {
        System.out.println("Test");
    }
}

Note there is no real functionality in these at this time.请注意,这些目前没有真正的功能。 Just trying to get the class import syntax correct before continuing.在继续之前,只是尝试使类导入语法正确。 Again while this seems to work as a package I want to have it done without using a package and importing ProcessPacket.java into NetTraffic.java.同样,虽然这似乎可以作为一个包工作,但我希望在不使用包并将 ProcessPacket.java 导入 NetTraffic.java 的情况下完成它。

public class NetTraffic {
    ProcessPacket pp = new ProcessPacket();
    pp.PrintOut();
}

You're calling the PrintOut() method outside of any constructor or method or similar block (static or non-static initializer blocks...), and this isn't legal.您在任何构造函数或方法或类似块(静态或非静态初始化块...)之外调用 PrintOut() 方法,这是不合法的。 Put it in a constructor or method.将它放在构造函数或方法中。

public class NetTraffic {
    public NetTraffic() {
        ProcessPacket pp = new ProcessPacket();
        pp.PrintOut();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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