• Post Reply Bookmark Topic Watch Topic
  • New Topic

Having a problem with a file monitor class

 
Curtis Fisher
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a file monitor class I have obtained from geosoft.no that I'm trying to work with. I want to monitor a directory then notify a listener when any file changes take place. I am having multiple issues with compiling and running the class. In eclipse I can compile but not run it under 1.4.2.06 and if I move the files off to the drive and hand compile them then it won't compile.

Here is what I get when I run in eclipse:
--------------------------------------------------------------------------
java.lang.UnsupportedClassVersionError: com/kcp/domain/FileMonitor (Unsupported major.minor version 49.0)
at java.lang.ClassLoader.defineClass0(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
Exception in thread "main"
---------------------------------------------------------------------------

Here is what I get when I try to compile from the drive:
---------------------------------------------------------------------------
C:\com\kcp\domain>javac FileMonitor.java
FileMonitor.java:92: cannot find symbol
symbol : class FileListener
location: class com.kcp.domain.FileMonitor
public void addListener (FileListener fileListener)
^
FileMonitor.java:114: cannot find symbol
symbol : class FileListener
location: class com.kcp.domain.FileMonitor
public void removeListener (FileListener fileListener)
^
FileMonitor.java:192: cannot find symbol
symbol : class FileListener
location: class com.kcp.domain.FileMonitor
private class TestListener implements FileListener {
^
FileMonitor.java:97: cannot find symbol
symbol : class FileListener
location: class com.kcp.domain.FileMonitor
FileListener listener = (FileListener) reference.get();
^
FileMonitor.java:97: cannot find symbol
symbol : class FileListener
location: class com.kcp.domain.FileMonitor
FileListener listener = (FileListener) reference.get();
^
FileMonitor.java:118: cannot find symbol
symbol : class FileListener
location: class com.kcp.domain.FileMonitor
FileListener listener = (FileListener) reference.get();
^
FileMonitor.java:118: cannot find symbol
symbol : class FileListener
location: class com.kcp.domain.FileMonitor
FileListener listener = (FileListener) reference.get();
^
FileMonitor.java:156: cannot find symbol
symbol : class FileListener
location: class com.kcp.domain.FileMonitor.FileMonitorNotifier
FileListener listener = (FileListener) reference.get();
^
FileMonitor.java:156: cannot find symbol
symbol : class FileListener
location: class com.kcp.domain.FileMonitor.FileMonitorNotifier
FileListener listener = (FileListener) reference.get();
^
Note: FileMonitor.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
9 errors
--------------------------------------------------------------------------

The FileMonitor code is below:
--------------------------------------------------------------------------
/**
* Class for monitoring changes in disk files.
* Usage:
*
* 1. Implement the FileListener interface.
* 2. Create a FileMonitor instance.
* 3. Add the file(s)/directory(ies) to listen for.
*
* fileChanged() will be called when a monitored file is created,
* deleted or its modified time changes.
*
* @author <a href="mailto:jacob.dreyer@geosoft.no">Jacob Dreyer</a>
*/
public class FileMonitor
{
private Timer timer_;
private HashMap files_; // File -> Long
private Collection listeners_; // of WeakReference(FileListener)


/**
* Create a file monitor instance with specified polling interval.
*
* @param pollingInterval Polling interval in milli seconds.
*/
public FileMonitor (long pollingInterval)
{
files_ = new HashMap();
listeners_ = new ArrayList();

timer_ = new Timer (true);
timer_.schedule (new FileMonitorNotifier(), 0, pollingInterval);
}



/**
* Stop the file monitor polling.
*/
public void stop()
{
timer_.cancel();
}


/**
* Add file to listen for. File may be any java.io.File (including a
* directory) and may well be a non-existing file in the case where the
* creating of the file is to be trepped.
* <p>
* More than one file can be listened for. When the specified file is
* created, modified or deleted, listeners are notified.
*
* @param file File to listen for.
*/
public void addFile (File file)
{
if (!files_.containsKey (file)) {
long modifiedTime = file.exists() ? file.lastModified() : -1;
files_.put (file, new Long (modifiedTime));
}
}



/**
* Remove specified file for listening.
*
* @param file File to remove.
*/
public void removeFile (File file)
{
files_.remove (file);
}



/**
* Add listener to this file monitor.
*
* @param fileListener Listener to add.
*/
public void addListener (FileListener fileListener)
{
// Don't add if its already there
for (Iterator i = listeners_.iterator(); i.hasNext(); ) {
WeakReference reference = (WeakReference) i.next();
FileListener listener = (FileListener) reference.get();
if (listener == fileListener)
return;
}

// Use WeakReference to avoid memory leak if this becomes the
// sole reference to the object.
listeners_.add (new WeakReference (fileListener));
}



/**
* Remove listener from this file monitor.
*
* @param fileListener Listener to remove.
*/
public void removeListener (FileListener fileListener)
{
for (Iterator i = listeners_.iterator(); i.hasNext(); ) {
WeakReference reference = (WeakReference) i.next();
FileListener listener = (FileListener) reference.get();
if (listener == fileListener) {
i.remove();
break;
}
}
}



/**
* This is the timer thread which is executed every n milliseconds
* according to the setting of the file monitor. It investigates the
* file in question and notify listeners if changed.
*/
private class FileMonitorNotifier extends TimerTask
{
public void run()
{
// Loop over the registered files and see which have changed.
// Use a copy of the list in case listener wants to alter the
// list within its fileChanged method.
Collection files = new ArrayList (files_.keySet());

for (Iterator i = files.iterator(); i.hasNext(); ) {
File file = (File) i.next();
long lastModifiedTime = ((Long) files_.get (file)).longValue();
long newModifiedTime = file.exists() ? file.lastModified() : -1;

// Chek if file has changed
if (newModifiedTime != lastModifiedTime) {

// Register new modified time
files_.put (file, new Long (newModifiedTime));

// Notify listeners
for (Iterator j = listeners_.iterator(); j.hasNext(); ) {
WeakReference reference = (WeakReference) j.next();
FileListener listener = (FileListener) reference.get();

// Remove from list if the back-end object has been GC'd
if (listener == null)
j.remove();
else
listener.fileChanged (file);
}
}
}
}
}


/**
* Test this class.
*
* @param args Not used.
*/
public static void main (String args[])
{
// Create the monitor
FileMonitor monitor = new FileMonitor (1000);

// Add some files to listen for
monitor.addFile (new File ("/testfiles/input/HelpLinks.xml"));


// Add a dummy listener
monitor.addListener (monitor.new TestListener());

// Avoid program exit
while (!false) ;
}


private class TestListener implements FileListener {
public void fileChanged (File file){
System.out.println ("File changed: " + file);
}
}
}
--------------------------------------------------------------------------

The Interface code is below:
--------------------------------------------------------------------------
/*
* This code is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This code is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program; if not, write to the Free
* Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
* MA 02111-1307, USA.
*/
package no.geosoft.cc.io;


import java.io.File;



/**
* Interface for listening to disk file changes.
* @see FileMonitor
*
* @author <a href="mailto:jacob.dreyer@geosoft.no">Jacob Dreyer</a>
*/
public interface FileListener
{
/**
* Called when one of the monitored files are created, deleted
* or modified.
*
* @param file File which has been changed.
*/
void fileChanged (File file);
}

Can anyone shed any light on this problem? I'll begin troubleshooting it, but it's a fairly straightforward class and what is happening is weird.

Thanks,

Curtis Fisher
 
Joe Ess
Bartender
Posts: 9362
11
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

java.lang.UnsupportedClassVersionError: com/kcp/domain/FileMonitor (Unsupported major.minor version 49.0)

Means that you are trying to run classes which were compiled for one VM in an incompatable VM (i.e. you can't run classes compiled with Java 1.5 in a 1.4.* VM). Either recompile com/kcp/domain/FileMonitor, find a compatable version or upgrade your VM.
The reason you can't hand compile your class is because no.geosoft.cc.io;FileListener isn't in the classpath. See the documentation for the Java SDK Development Tools for how the classpath is set and how the compiler finds classes.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!