1

Here I have attached the source code and make file of it. I use netbeans. How should I build my project to execute this java code in netbeans. please help me with detailed steps. I am new to netbeans and java. I use netbeans 8.0.2 for windows 10 64 bit OS.

Source code:

package net.sourceforge.jpcap.tutorial.example15;

import net.sourceforge.jpcap.capture.*;
import net.sourceforge.jpcap.net.*;

/*
 *  This example utilizes the endCapture() feature.
 */
public class Example15 {
    private static final int INFINITE = -1;
    private static final int PACKET_COUNT = INFINITE;

    // BPF filter for capturing any packet
    private static final String FILTER = "";

    private PacketCapture m_pcap;
    private String m_device;


    public Example15() throws Exception {
      // Step 1:  Instantiate Capturing Engine
      m_pcap = new PacketCapture();

      // Step 2:  Check for devices 
      m_device = m_pcap.findDevice();

      // Step 3:  Open Device for Capturing (requires root)
      m_pcap.open(m_device, true);

      // Step 4:  Add a BPF Filter (see tcpdump documentation)
      m_pcap.setFilter(FILTER, true);

      // Step 5:  Register a Listener for Raw Packets        
      m_pcap.addRawPacketListener(new RawPacketHandler(m_pcap));

      // Step 6:  Capture Data (max. PACKET_COUNT packets)
      m_pcap.capture(PACKET_COUNT);
    }

    public static void main(String[] args) {
      try {
        Example15 example = new Example15();
      } catch(Exception e) {
        e.printStackTrace();
        System.exit(1);
      }
    }    
}


class RawPacketHandler implements RawPacketListener 
{
  private static int m_counter = 0;
  private PacketCapture m_pcap = null;

  public RawPacketHandler(PacketCapture pcap) {
    m_counter = 0;
    m_pcap = pcap;
  }

  public synchronized void rawPacketArrived(RawPacket data) {
    m_counter++;
    System.out.println("Packet " + m_counter + "\n" + data + "\n");

    if(condition())
      m_pcap.endCapture();
  }

  private boolean condition() {
    return (m_counter == 5) ? true : false;
  }
}

make file:

# $Id: makefile,v 1.1 2002/07/10 23:05:26 pcharles Exp $
#
#   package net.sourceforge.jpcap.tutorial.example15
#
PKG = net.sourceforge.jpcap.tutorial.example15
PKG_DIR = $(subst .,/, $(PKG))
REL = ../../../../..
include ${MAKE_HOME}/os.makefile
include ${MAKE_HOME}/rules.makefile

JAVA = \
    Example15

JAVA_SOURCE = $(addsuffix .java, $(JAVA))
JAVA_CLASSES = $(addsuffix .class, $(JAVA))

all: $(JAVA_CLASSES)

include ${MAKE_HOME}/targets.makefile
include ${MAKE_HOME}/depend.makefile
seenukarthi
  • 8,241
  • 10
  • 47
  • 68

1 Answers1

0

Netbeans uses Makefiles for C++ code but not for Java code. It is easy to get this code to build but there is no need for the Makefile.

  1. File -> New Project
  2. Select Category Java on the left and "Java Application with Existing Sources" (with this option the project and sources will be in different directories) on the right.
  3. Click Next
  4. Change the Project name and/or directory to create the project in.
  5. Add the original source directory in the dialog.
  6. Click Finish to Create the project.
  7. Within netbeans you can now use the Run-> Build Project to build it.
  8. If you really have to have a Makefile just make one that just runs the Netbeans project( which is actually an ant project).

eg.

build: 
    ant jar
WillShackleford
  • 6,918
  • 2
  • 17
  • 33
  • Slight improvement of the above: 1. File -> New Project 2. Select Category Java on the left and 'Java **Project with Existing Sources'** on the right. 3. Click Next 4. Change the Project name and/or directory to create the project in. 5. Click Finish to Create the project. 6. Within NetBeans you can now use the Run-> Build Project to build it. – OliCoder Sep 02 '15 at 13:08