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?
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?
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?
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();
}
}
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?