Clam4j should be fairly simple to use in most java applications, there are two parts to getting clam4j to work.
Clam4j is a wrapper around ClamAV, this means that ClamAV must already be installed and working (though there is no requirement for ClamAV and Clam4j to be running on the same machine).
The first step is to initalise Clam4j, this can be done with the following code:
java.util.Properties clam4jProps = new java.util.Properties();
clam4jProps.setProperty(ClamManager.PROP_CLAMD_HOST, "localhost");
clam4jProps.setProperty(ClamManager.PROP_CLAMD_PORT, "3310");
ClamManager.initalise(clam4jProps);
Thats is, Clam4j can now be used to scan for viruses
Once Clam4j has been initalised, its usage is quite simple and can be done with:
ClamScanner tScanner = new ClamScanner();
try
{
ClamScanResult tResult = tScanner.scanStream(pInputStream);
if (tResult.isThreadFound())
{
System.out.println("Virus [" + tResult.getThreatName() + "] detected!");
}
else
{
System.out.println("No threats found!");
}
}
catch (ClamException tEx)
{
System.out.println("Oh Dear:" + tEx.toString());
tEx.printStackTrace();
}
Clam4j can help look after the Clam virus database too. This is done by running the freshclam program on a defined schedule, you choose if you want this when you initalise Clam4j. This is currently only supported where the clamd and your Java application are running on the same physical machine and the Java process can spawn freshclam in a way that allows it to update the virus database.
To initalise Clam4j enabling freshclam you will need to do the following:
java.util.Properties clam4jProps = new java.util.Properties();
clam4jProps.setProperty(ClamManager.PROP_CLAMD_HOST, "localhost");
clam4jProps.setProperty(ClamManager.PROP_CLAMD_PORT, "3310");
clam4jProps.setProperty(ClamManager.PROP_RUN_UPDATE, "true");
clam4jProps.setProperty(ClamManager.PROP_UPDATE_FREQ_MIN, "60"); // Once every 60 minutes
clam4jProps.setProperty(ClamManager.PROP_FRESHCLAM_PATH, "/path/to/freshclam/executable");
//optionally set a callback so your application can be notified of freshclam runs
clam4jProps.setProperty(ClamManager.PROP_FRESHCLAM_CALLBACK, "class.that.extends.FreshClamCallback");
ClamManager.initalise(clam4jProps);