NoSuchFileException when running jar file - java

When I run a jar file that accesses other files in the src directory, I get the following RuntimeException:
Exception in thread "main" java.nio.file.NoSuchFileException:
src\FileToBeAccessed.txt.lck
at sun.nio.fs.WindowsException.translateToIOException(Unknown Source)
at sun.nio.fs.WindowsException.rethrowAsIOException(Unknown Source)
at sun.nio.fs.WindowsException.rethrowAsIOException(Unknown Source)
at sun.nio.fs.WindowsFileSystemProvider.newFileChannel(Unknown Source)
at java.nio.channels.FileChannel.open(Unknown Source)
at java.nio.channels.FileChannel.open(Unknown Source)
at java.util.logging.FileHandler.openFiles(Unknown Source)
at java.util.logging.FileHandler.(Unknown Source)
at Program.main(Program.java:30)
The code works fine when I run it directly without building a jar file.

You'll need to change how you access resources in a jar to a stream.
this.getClass().getResourceAsStream("/file.txt");

The correct way is
this.getClass().getResourceAsStream("file.txt");

It is really strange that
this.getClass().getResource("file.txt")
does work in IDE but NOT when you run it from a jar.
this.getClass().getResourceAsStream("file.txt")
works both inside IDE and from jar.

Related

JavaFx Location is not set when application is compiled to a jar file

My program runs fine when I run it on eclipse but when I package it in a jar file, I get this error when I run it.
java.lang.IllegalStateException: Location is not set.
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.load(Unknown Source)
at controller.MainController.initAnalyzer(MainController.java:144)
at applicationMain.Main.start(Main.java:32)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(Unknown Source)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(Unknown Source)
at java.lang.Thread.run(Unknown Source)
I am certain that the path is correct. I have tried to change the path but nothing seems to work. I've also checked answers here and from other websites but nothing seems to solve my problem. It just does not make sense that my other FXML files under the same folder loaded well but not these three.
I think of the two following ways:
Make sure you set the path correctly. Anyway you should show us the folder system and how you set the path. Don't forget that the path starts from the current package, not the root of the project.
Clean and Build your project and run it again. It often somehow helps me to solve lots of issues.

Problems exporting Jar using Weka

i've got a big problem exporting my project as a jar file (inside my project i use weka). I'm using Eclipse.
I've added in Java Build Path weka.jar and i've attached to it weka-src.jar, testing my classifier under eclipse everything works, so i've decided to create a jar (because i need that another program use it).
First of all i've created the manifest file:
Manifest-Version: 1.0
Rsrc-Class-Path: ./ weka.jar weka-src.jar
Class-Path: .
Rsrc-Main-Class: start.Home
Main-Class: start.Home
The main class of the program is Home (inside the package start), while weka.jar and weka-src.jar are inside the /src/ directory of the eclipse program.
When i try to execute the jar created i receive this output:
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: weka/classifiers/Classifier
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
at java.lang.Class.privateGetMethodRecursive(Unknown Source)
at java.lang.Class.getMethod0(Unknown Source)
at java.lang.Class.getMethod(Unknown Source)
at sun.launcher.LauncherHelper.validateMainClass(Unknown Source)
at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
Caused by: java.lang.ClassNotFoundException: weka.classifiers.Classifier
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)
... 7 more
I think that the problem is the CLASSPATH so i've tried to solve it in this way:
I've created the Environment Variable CLASSPATH with this values:
C:\Users\tomma\Desktop\workspace Eclipse\AndroidPermissionsClassifier\src\weka.jar
C:\Users\tomma\Desktop\workspace Eclipse\AndroidPermissionsClassifier\src\weka-src.jar
but nothing changed, can someone help me please?
You are right: the classpath is the problem. Did you remove the items from the manifest classpath when you added them to the environment CLASSPATH? (Side note: place the source jar in the -sourcepath, not the classpath.)

Trouble creating a runnable JAR from Eclipse

I'm trying to create a runnable JAR from https://bitbucket.org/madsen953/ethervisu in Eclipse. When I try to run it I get:
Exception in thread "Monitor" java.lang.UnsatisfiedLinkError: no jnetpcap in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1864)
at java.lang.Runtime.loadLibrary0(Runtime.java:870)
at java.lang.System.loadLibrary(System.java:1122)
at org.jnetpcap.Pcap.<clinit>(Unknown Source)
at ethervisu.monitors.JNetPcapMonitor.run(JNetPcapMonitor.java:28)
at java.lang.Thread.run(Thread.java:745)
java.lang.NullPointerException
at java.io.DataInputStream.readInt(DataInputStream.java:387)
at processing.core.PFont.<init>(Unknown Source)
at processing.core.PApplet.loadFont(Unknown Source)
at jgv.graphics.JGVGraphics$GraphVisuApplet.setup(JGVGraphics.java:80)
at processing.core.PApplet.handleDraw(Unknown Source)
at processing.core.PApplet.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
Exception in thread "Animation Thread" java.lang.RuntimeException: Could not load font /data/ArialMT-48.vlw. Make sure that the font has been copied to the data folder of your sketch.
at processing.core.PApplet.die(Unknown Source)
at processing.core.PApplet.die(Unknown Source)
at processing.core.PApplet.loadFont(Unknown Source)
at jgv.graphics.JGVGraphics$GraphVisuApplet.setup(JGVGraphics.java:80)
at processing.core.PApplet.handleDraw(Unknown Source)
at processing.core.PApplet.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
I think this is because I'm unable to preserve the directory structure when creating the JAR. The font files are at the root instead of the data directory. How can I fix this?
try to check the path of JDK set properly pointing to correct library of java in buildpath.
and make sure correct jdk version is installed. no mess up with x86/x64 installs.`
The solution was to put the font files in a directory named data in the same directory as the jar and run it with java -Djava.library.path=jnetpcap-1.3.0 -jar thejar.jar where jnetpcap-1.3.0 is a directory where jnetpcap with its associated .so resides.

Java - Unable to launch the application

I can't seem to launch a .jar file. An error comes up when I launch it. Help would be appreciated, here is the error:
CouldNotLoadArgumentException[ Could not load file/URL specified: C:\Users\Donavon\AppData\Local\Temp\javaws12]
at com.sun.javaws.Main.launchApp(Unknown Source)
at com.sun.javaws.Main.continueInSecureThread(Unknown Source)
at com.sun.javaws.Main$1.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
First, please check if your TEMP folder is still there.
Second, if you can find jawaaws12 in there, move it to somewhere else. Then uninstall and re-install java. The appropriate file should be created automatically.

Java get resource InvalidJarIndexException

I have a class names test.java and another file "log4j.properties" in the same folder but following call
getClass().getResource("log4j.properties")
fails with
ms
Trace:
sun.misc.InvalidJarIndexException: Invalid index
at sun.misc.URLClassPath$JarLoader.getResource(Unknown Source)
at sun.misc.URLClassPath$JarLoader.getResource(Unknown Source)
at sun.misc.URLClassPath$JarLoader.findResource(Unknown Source)
at sun.misc.URLClassPath.findResource(Unknown Source)
at java.net.URLClassLoader$2.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findResource(Unknown Source)
at java.lang.ClassLoader.getResource(Unknown Source)
at java.lang.ClassLoader.getResource(Unknown Source)
at java.lang.Class.getResource(Unknown Source)
But getClass().getClassLoader().getResource("log4j.properties") works. What can be the reason?
The InvalidJarIndexException is normally caused by a corrupt INDEX.LIST file in the JAR. So check the JAR for any problems. See http://littletechsecrets.wordpress.com/2008/12/01/why-does-invalidjarindexexception-occur/
The different results you describe are probably related to Class.getResource() resolving a path relative to the classes package, and ClassLoader.getResource() resolving a path relative to the root. So getClass().getResource("/log4j.properties") might fix your problem.

Categories