简体   繁体   中英

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...

I am building a project in intellij for class. This project imports jnetcap and uses it to process a captured pcap file. 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.

I have tried to make a project and import ProcessPacket into NetPacket but have been unsuccessful so far. 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. 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. 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.

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?

The files in question reside in package named nettraffic in src directory.

NetTraffic.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.

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. Put it in a constructor or method.

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

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