Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Listener Error. Cannot Compile

Status
Not open for further replies.

Milleniumlegend

IS-IT--Management
Dec 16, 2003
135
I am trying to create a new object but when compiling I am getting the following error message.

Could you please let me know what could be the reason.

The code is as follows.
Code:
private FileSystemWatcher _fileSystemWatcher;
void StartWatching()
{
 _fileSystemWatcher = new FileSystemWatcher(PATH_TO_FILE,   true);
 _fileSystemWatcher.addFileSystemListener(this);
}

The method is as follows in the API.

void addFileSystemListener(FileSystemEventListener listener)

Could you please provide some pointers.

Thanks
 
Code:
public class FSWatch {

//private static final Logger LOG = Logger.getInstance(FileSystemWatcherSample.class);

static final String[] ACTION_NAMES = new String[]{"added", "removed", "modified", "renamed"};
static final String MESSAGE_TEMPLATE = "File ''{0}'' was {1}";

static final String SHORT_DATE_FORMAT = "HH:mm:ss";
static final String LONG_DATE_FORMAT = "dd/MM/yyyy HH:mm:ss";

static final String PATH_TO_FILE = "Q:\\Oscar";

private FileSystemWatcher _fileSystemWatcher;

private FileSystemEventListener _fslisten; 


String getDateTime(boolean longFormat)
{
SimpleDateFormat dateFormat = new SimpleDateFormat(longFormat ? LONG_DATE_FORMAT : SHORT_DATE_FORMAT);
return dateFormat.format(new Date());
}
	
void StartWatching()
{
_fileSystemWatcher = new FileSystemWatcher(PATH_TO_FILE, true);
_fileSystemWatcher.addFileSystemListener(_fslisten);
_fileSystemWatcher.start();

FileSystemWatcher.WatcherOptions options = _fileSystemWatcher.getOptions();

System.out.println ("options");


}

public static void main (String [] args){

}

}

This is the code and I have got the code to compile somehow. What I dont understand is that the options does return a value which I am not able to interpret. 

I cant print the options in a system.out.print statement.
 
What is the API for these two object?

private FileSystemWatcher _fileSystemWatcher;
private FileSystemEventListener _fslisten;

Do you define them yourself, or from a standard API?


Chinese Java Faq Forum
 
I define them from the Standard API.

I got them to compile but when I run them it gives me a runtime error.

Code:
C:\WinPack\samples>java -cp C:\winpack\lib\winpack-3.0.jar; FileSystemWatcherSample
Watched folder = C:\
Exception in thread "main" java.lang.NoClassDefFoundError: com/jniwrapper/util/Logger at com.jniwrapper.win32.io.FileSystemWatcher.<clinit>(FileSystemWatcher.java:34)
at FileSystemWatcherSample.main FileSystemWatcherSample.java:43)
 
The following is the new sample code.

Code:
import com.jniwrapper.win32.io.FileSystemEvent;
import com.jniwrapper.win32.io.FileSystemEventListener;
import com.jniwrapper.win32.io.FileSystemWatcher;

import java.io.File;
import java.text.MessageFormat;

/**
 * Sample for FileSystemWatcher. All watcher messages are put into console until the messagebox is closed
 * First command line parameter is folder to be watched. If it is not specified the current folder will be used.
 *
 * @author Serge Piletsky
 */
public class FileSystemWatcherSample
{
    static final String[] ACTION_NAMES = new String[]{"added", "removed", "modified", "renamed"};
    static final String MESSAGE_TEMPLATE = "File ''{0}'' was {1}";

    private FileSystemWatcherSample()
    {
    }

    public static void main(String[] args)
    {
        String folder = new File("C:/").getAbsolutePath();
        if (args.length > 0)
        {
            File f = new File(args[0]);
            if (f.exists())
            {
                folder = args[0];
            }
        }
        System.out.println("Watched folder = " + folder);
        FileSystemWatcher watcher = new FileSystemWatcher(folder, true);
        watcher.addFileSystemListener(new FileSystemEventListener()
        {
            public void handle(FileSystemEvent event)
            {
                int action = event.getAction();
                String actionName = ACTION_NAMES[action - 1];
                String message = MessageFormat.format(MESSAGE_TEMPLATE, new Object[]{event.getFileInfo(), actionName});
                if (action == FileSystemEvent.FILE_RENAMED)
                {
                    message += " from '" + event.getOldFileInfo() + "'";
                }

                System.out.println(message);
            }
        });

        try
        {
            watcher.start();
            System.out.println("Watching stared. Press 'Enter' to stop the watching.");
            System.in.read();
            watcher.stop();
            System.out.println("Watching stopped");
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}
 
I am not sure waht causes the problem. But the sound likes the class path problem to me. Please ignore me if I am wrong.

change
import com.jniwrapper.win32.io.FileSystemWatcher;
to
import com.jniwrapper.win32.io.FileSystemWatcher.*;


Chinese Java Faq Forum
 
wangdong : That will NOT allow the code to compile - infact it will make things worse.

The error is actually (as hinted at before) a runtime CLASSPATH error. The OP says they CAN compile it.

This error :

java.lang.NoClassDefFoundError: com/jniwrapper/util/Logger

tells us what the actual problem is : the jar file that contains the com.jniwrapper packages is missing from the CLASSPATH.
Add the relevant jar (I imagine one of the jars downloaded from jniwrapper.com), and you will be fine.

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Code:
C:\winpack\samples>java -cp C:\winpack\lib\winpack-3.0.jar; FileSystemWatcherSample C:\temp

Watched folder = C:\temp
Exception in thread "main" java.lang.NoClassDefFoundError: com/jniwrapper/util/Logger at 

com.jniwrapper.win32.io.FileSystemWatcher.<clinit>(FileSystemWatcher.java:34)at FileSystemWatcherSample.main

(FileSystemWatcherSample.java:43)
When I compile using the string
Code:
javac -classpath C:\winpack\lib\winpack-3.0.jar; FileSystemWatcherSample.java

The compile Works fine. But for some reason the program would not work. Could you please tell me what do I need to do to get this working.
 
I have looked for the class util.Logger but could not find that anywhere.

Thanks for your cooperation.

 
Thanks sedj. Thats exactly what i was looking for. Sorry I had overlooked the jar file. I had to request these guys to send me a license file for this to work.

It seems to be working at the moment.

Appreciate your help.

Cheers
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top