Netbeans imported JAR not loading on run - java

i see this question has been posted many times but it has been solved with adding
-Djava.library.path="./path" to the VM runtime options.
I have to build an app in JAVA which uses the JNotify classes.
this is the sample code:
package test;
import net.contentobjects.jnotify.JNotify;
import net.contentobjects.jnotify.JNotifyListener;
/**
*
* #author
*/
public class Test {
public void jnotifydemo() throws Exception {
// path to watch
String path = System.getProperty("user.home");
// watch mask, specify events you care about,
// or JNotify.FILE_ANY for all events.
int mask = JNotify.FILE_CREATED
| JNotify.FILE_DELETED
| JNotify.FILE_MODIFIED
| JNotify.FILE_RENAMED;
// watch subtree?
boolean watchSubtree = true;
// add actual watch
int watchID = JNotify.addWatch(path, mask, watchSubtree, new Listener());
// sleep a little, the application will exit if you
// don't (watching is asynchronous), depending on your
// application, this may not be required
Thread.sleep(1000000);
// to remove watch the watch
boolean res = JNotify.removeWatch(watchID);
if (!res) {
// invalid watch ID specified.
}
}
class Listener implements JNotifyListener {
public void fileRenamed(int wd, String rootPath, String oldName,
String newName) {
print("renamed " + rootPath + " : " + oldName + " -> " + newName);
}
public void fileModified(int wd, String rootPath, String name) {
print("modified " + rootPath + " : " + name);
}
public void fileDeleted(int wd, String rootPath, String name) {
print("deleted " + rootPath + " : " + name);
}
public void fileCreated(int wd, String rootPath, String name) {
print("created " + rootPath + " : " + name);
}
void print(String msg) {
System.err.println(msg);
}
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) throws Exception {
System.out.println("Hello World");
new Test().jnotifydemo();
}
}
When i run this i get:
Error loading library, java.library.path=C:\Program Files\Java\jdk1.6.0_26\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:\Windows\system32;(continues)
Exception in thread "main" java.lang.UnsatisfiedLinkError: no jnotify in java.library.path
I have setup a Netbeans project and added the JAR file to the project so that the JAR is correctly in the lib/ folder of my project and everything is set in NETBEANS.
This correctly works if is setup the -Djava.library.path="./path" argument of the java VM, but if i imported my lib in NETBEANS that should be included in the path automatically.
I am doing something wrong or it is necessary to put every .jar in the classpath system variable? I would like to release this app so it can run on other systems that does not have JNotify in their libs.
Thanks
I am using Netbeans 7.2 on Win 7 32Bit

You are messing java jar files as library which has to be added only in netbeans classpath:
Simply in NetBeans on project properties click and adjust Library having your JAR file.
For the native libraries (so,dll,...) you need to have set: -Djava.library.path. As you did in your question.
So you have 2 steps:
1. from http://sourceforge.net/projects/jnotify/files/jnotify/jnotify-0.94/jnotify-lib-0.94.zip/download add jnotify-0.94.jar to your libraries as in picture above (this will update your classpath automatically)
2. jnotify.dll, or jnotify_64bit.dll for 64-bit windows place is some directory and ad this to your -Djava.library.path - add this to VM option of the projects property

Related

How to check if file exist after build jlink?

I am trying to check if file exists in project. When I start my application through Intellij idea and check if file exists it's return true but when I create jlink build and start it through .bat and check if file exists it's always return false.
public class App {
public static String getPathToDllTest(String filename) throws URISyntaxException {
return Paths.get(App.class.getResource("files/" + filename + ".txt").toURI()).toString();
}
public static void main(String[] args) throws Exception {
File txt = new File(App.getPathToDllTest("test"));
System.out.println(txt.exists());
}
}
Here is basic Maven project resources structure I use:
src
main
java
com
example
App.java
resources
com
example
files
test.txt
Is there problem in path? How can I fix it?

Exception found java "java.awt.AWTError:Assistive Technology not found"

My application running on JRE-6 64-bit. I enable Java's assistive technology using:
assistive_technologies=com.sun.java.accessibility.AccessBridge
But the files for JavaAccessBridge-64.dll, JAWTAccessBridge-64.dll, and WindowsAccessBridge-64.dll are not present in JRE 6 path.
I try to customize "java.library.path" path ,i download java access bridge and append folder path in "java.library.path".
I write below code ,
static Toolkit tk ;
static long eventMask = AWTEvent.MOUSE_MOTION_EVENT_MASK + AWTEvent.MOUSE_EVENT_MASK + AWTEvent.KEY_EVENT_MASK;
public static void main(String[] args)
{
try {
String te= "D:\\AccessBridge" + File.pathSeparator + System.getProperty("java.library.path");
System.setProperty("java.library.path",te);
String library =System.getProperty("java.library.path");
tk = Toolkit.getDefaultToolkit();
} catch (Exception e1) {
e1.printStackTrace();
}
tk.addAWTEventListener(new AWTEventListener()
{
#Override
public void eventDispatched(AWTEvent e)
{
System.out.println(e.getID() + ", " + e);
}
}, eventMask);
}
Found a below exception.
Exception in thread "main" java.awt.AWTError: Assistive Technology not found: com.sun.java.accessibility.AccessBridge
at java.awt.Toolkit.loadAssistiveTechnologies(Toolkit.java:773)
at java.awt.Toolkit.getDefaultToolkit(Toolkit.java:872)
at Automation.MyToolKit.main(MyToolKit.java:73)
Is it possible load this dll from different location instead of copy in to JRE folder?

Spark compiles with Eclipse on Windows but not javac on CentOS VM

I am trying to compile the spark example from
https://spark.apache.org/docs/latest/quick-start.html
/* SimpleApp.java */
import org.apache.spark.api.java.*;
import org.apache.spark.SparkConf;
import org.apache.spark.api.java.function.Function;
public class Sparktest {
public static void main(String[] args) {
String logFile = args[0] + "/README.md"; // Should be some file on your system
SparkConf conf = new SparkConf().setAppName("Simple Application");
JavaSparkContext sc = new JavaSparkContext(conf);
JavaRDD<String> logData = sc.textFile(logFile).cache();
long numAs = logData.filter(new Function<String, Boolean>() {
public Boolean call(String s) { return s.contains("a"); }
}).count();
long numBs = logData.filter(new Function<String, Boolean>() {
public Boolean call(String s) { return s.contains("b"); }
}).count();
System.out.println("Lines with a: " + numAs + ", lines with b: " + numBs);
}
}
if I copy the code into eclipse and add the
spark-assembly-1.3.1.2.3.0.0-2130-hadoop2.7.1.2.3.0.0-2130.jar
file to the build path through eclipse everything resolves and eclipse places my class files in the projects bin directory.
When I move the code over to my CentOS VM and execute the following line
javac -cp ./spark-assembly-1.3.1.2.3.0.0-2130-hadoop2.7.1.2.3.0.0-2130.jar:. ./Sparktest.java
in a directory with the jar file it complains about
package org.apache.spark.api does not exist
package org.apache.spark does not exist
package org.apache.spark.api.java.function does not exist
I have opened the jar file with 7zip on Windows, and know that that directory structure exists and all the class are in the jar. I'm still new to command line compiling java, but am pretty sure that there is some kind of directory structure thing going on here.
What combination of a javac command and directory structure do I need to get this to compile?

JAVA - Access file from shared storage without mount

I have done set up a NAS server using NAS4Free and share a folder at:
\\NAS_SERVER_IP/SHARE_FOLDER_NAME
In SHARE_FOLDER_NAME directory contains resource files need to share to multiple clients
Now ,from clients , can I using Java to access (read/write) directly file from NAS server without mount shared folder to local clients
Copied from here, but changed the api call argument.
connecting to shared folder in windows with java
String url = "smb://[NAS server-IP or hostname]/file-or-directory-path";
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("[company network domain]", "user", "password");
SmbFile dir = new SmbFile(url, auth);
for (SmbFile f : dir.listFiles())
{
System.out.println(f.getName());
}
For observing file/dir changes using JDK 6, you could use:
WatchService for Java 6
For JDK 7, WatchService is part of NIO package:
http://java.dzone.com/news/how-watch-file-system-changes
Finally, this one works with JDK6 as well. This way, we could observe file/dir changes in windows shared drivers without mounting/mapping them as a drive.
I've used following jars in classpath: commons-collections-4.4.0, commons-logging-1.1.2, commons-logging-api-1.1.2, commons-net-3.3, commons-vfs2-2.0, httpclient-4.3.1, jackrabbit-standalone-2.6.5, jcifs-1.3.17, jsch-0.1.51
import org.apache.commons.vfs2.FileChangeEvent;
import org.apache.commons.vfs2.FileListener;
import org.apache.commons.vfs2.FileObject;
import org.apache.commons.vfs2.FileSystemException;
import org.apache.commons.vfs2.FileSystemManager;
import org.apache.commons.vfs2.VFS;
import org.apache.commons.vfs2.impl.DefaultFileMonitor;
public class NFSChangeObserver
{
public static void main(String[] args) throws FileSystemException
{
/** need a non-daemon thread, because <code>DefaultFileMonitor</code> is internally marked as a daemon thread.
*/
Thread t = new Thread(new Runnable() {
#Override
public synchronized void run()
{
try
{
while(1!=2)
wait();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}});
t.start();
FileSystemManager manager = VFS.getManager();
FileObject file = manager.resolveFile("\\\\[server-hostname]\\[directory-path]");
DefaultFileMonitor fm = new DefaultFileMonitor(new FileListener()
{
#Override
public void fileChanged(final FileChangeEvent fileChangeEvt) throws Exception
{
System.out.println("#" + System.currentTimeMillis() + ": " + fileChangeEvt.getFile().getName() + " changed .." );
}
#Override
public void fileCreated(FileChangeEvent fileChangeEvt) throws Exception
{
System.out.println("#" + System.currentTimeMillis() + ": " + fileChangeEvt.getFile().getName() + " created .." );
}
#Override
public void fileDeleted(FileChangeEvent fileChangeEvt) throws Exception
{
System.out.println("#" + System.currentTimeMillis() + ": " + fileChangeEvt.getFile().getName() + " deleted .." );
}
});
fm.setDelay(5000);
fm.addFile(file);
FileObject[] children = file.getChildren();
for(FileObject child : children)
{
System.out.println(child.getURL());
}
fm.start();
}
}

Load file in different directory

I want to load xml configuration file located in different directory.
The location of the files is:
<location_dir>/jar_file.jar
<location_dir>/xml_file.xml
The files are located in different directories which are located into one main directory. The different part is that I want want to sue the same directory structure on many operating systems.
I managed to create this code:
public class SeparatorTest
{
private static SettingsDataObject settingsData;
public void loadXMLFile() throws JAXBException
{
try
{
// File location by default ../conf/xmlfile.xml
settingsData = unmarshal(".." + File.separator + "conf" + File.separator + "xmlfile.xml");
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
}
}
I want to ask is this example universal for every OS?

Categories

Resources