• Post Reply Bookmark Topic Watch Topic
  • New Topic

Unexpected error by reading a file through a cmd window  RSS feed

 
Christian Klugesherz
Ranch Hand
Posts: 103
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all

Strange behavior.
File "Abreviation.html" is located at:
D:\Users\kluges1\workspace\pac-tool\bin\gui\html\Abreviation.html

By executing the following code under Eclipse all is working well.
Frame opens, and I can read the content of "Abreviation.html"



--> "Output" in Eclipse Console is giving



Now when I run the code under Windows cmd console,  I got an error linked to the fact that java is trying to open

file:/D:/Users/kluges1/workspace/pac-tool/jar/WinDefinition.jar!/gui/html/Abreviation.html

D:\Users\kluges1\workspace\pac-tool\jar>java -jar WinDefinition.jar

file:/D:/Users/kluges1/workspace/pac-tool/jar/WinDefinition.jar!/gui/html/Abrevi
ation.html
00:04:14.396 ERROR gui.WinDefinition - Ops! (WinDefinition)
java.io.FileNotFoundException: file:\D:\Users\kluges1\workspace\pac-tool\jar\Win
Definition.jar!\gui\html\Abreviation.html (La syntaxe du nom de fichier, de rΘpe
rtoire ou de volume est incorrecte)
        at java.io.FileInputStream.open0(Native Method) ~[?:1.8.0_121]
        at java.io.FileInputStream.open(Unknown Source) ~[?:1.8.0_121]
        at java.io.FileInputStream.<init>(Unknown Source) ~[?:1.8.0_121]
        at java.io.FileReader.<init>(Unknown Source) ~[?:1.8.0_121]
        at gui.WinDefinition.<init>(WinDefinition.java:82) [WinDefinition.jar:?]

        at gui.WinDefinition$1.run(WinDefinition.java:59) [WinDefinition.jar:?]
        at java.awt.event.InvocationEvent.dispatch(Unknown Source) [?:1.8.0_121]

        at java.awt.EventQueue.dispatchEventImpl(Unknown Source) [?:1.8.0_121]
        at java.awt.EventQueue.access$500(Unknown Source) [?:1.8.0_121]
        at java.awt.EventQueue$3.run(Unknown Source) [?:1.8.0_121]
        at java.awt.EventQueue$3.run(Unknown Source) [?:1.8.0_121]
        at java.security.AccessController.doPrivileged(Native Method) ~[?:1.8.0_
121]
        at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionP
rivilege(Unknown Source) [?:1.8.0_121]
        at java.awt.EventQueue.dispatchEvent(Unknown Source) [?:1.8.0_121]
        at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) [
?:1.8.0_121]
        at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) [?:1
.8.0_121]
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) [
?:1.8.0_121]
        at java.awt.EventDispatchThread.pumpEvents(Unknown Source) [?:1.8.0_121]

        at java.awt.EventDispatchThread.pumpEvents(Unknown Source) [?:1.8.0_121]

        at java.awt.EventDispatchThread.run(Unknown Source) [?:1.8.0_121]

D:\Users\kluges1\workspace\pac-tool\jar>



What did I wrong ?

Thanks

Christian
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
getResource looks on the classpath to find files
The classpath for a program executed in a jar file is inside the jar file:
D:\Users\kluges1\workspace\pac-tool\jar\WinDefinition.jar!\gui\html\Abreviation.html <<<  Note the !
The path up to the ! represents the jar file.
The path to the resource was added to the end of the classpath.

Can the file be placed in the jar file where getResource will work?
Or if the file must be outside of the jar file, don't treat the file as a resource
 
Christian Klugesherz
Ranch Hand
Posts: 103
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm sorry I didn't catch.

In eclipse project it is the case.
See the Java Build Path : classpath folder.

Why


--> is not working


--> working


Thanks
Capture.PNG
[Thumbnail for Capture.PNG]
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
00:04:14.396 ERROR gui.WinDefinition - Ops! (WinDefinition) 

Add a call to the printStackTrace() method in the catch block to get the full text of the error message. 
Does the logger show the stack trace?

I get a NPE at the call to the getFile() method when I execute the code.
 
Dave Tolls
Ranch Foreman
Posts: 3056
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The stack trace is in the OP, I think.
Most logging frameworks print it out if handed an exception.

It's a FileNotFound.
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.  I suspected that could be. 

Why do I get a NPE with the code vs FileNotFound?
 
Christian Klugesherz
Ranch Hand
Posts: 103
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please could you clarify how to fix ?
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the file you want to read is a resource, move it into the jar file.
If the file can change between executions of the program and can not be in the jar file, do not treat it as a resource.  Use normal paths to read the file. Do not use getResource
 
Christian Klugesherz
Ranch Hand
Posts: 103
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry,

Is this not what I'm done in Eclipse through java build path.
Sorry to ask, I'm really not see how to fix

Thanks for your help

 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christian Klugesherz wrote:
Why


--> is not working


--> working

Abreviation.html is a file on your file system which isn't bundled in the jar you are running from the command line as so it can't be found with getResource(..).
PAC-Tool_16.png is a file that is bundled in the jar you are running from the command line as so it can be found with getResource(..).

As Norm has already said you have two choices:
1. Add Abreviation.html to your jar - but you can only realistically do this if the file doesn't change.
2. Change your code to create a File object using a absolute path or, probably better, a path relative to the location of the jar.

The reason your code works in Eclipse is it doesn't know what you will be bundling in the final jar so it assumes everything on the build path will be put into the jar.
 
Christian Klugesherz
Ranch Hand
Posts: 103
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Tony,

Many thanks for this clear clarification.
Now I understood.
So by default, Eclipse will bundle in the jar:
  automatically all images *.png files so (PAC-Tool_16.png)
  and not *.html so not Abreviation.html
even there is no indication that "packages" gui.images and gui.html will be bundled in jar
See picture below

For your information: Abreviation.html will never change (it is like and user guide which is linked to a release, so fix file)

So to fix I have in Eclipse :
to right-click on Abreviation.html and "Build path" --> "add to build path"

 
Christian Klugesherz
Ranch Hand
Posts: 103
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not working :-(
The operation above, moved Abreviation.htm file to "Referenced Library"
By sense I changed code


but creation of jar is no more possible, certainly due to !
Capture.PNG
[Thumbnail for Capture.PNG]
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You do not use a File object with a resource that is located in a jar file.
What other classes and methods can be used to retrieve the file?
 
Christian Klugesherz
Ranch Hand
Posts: 103
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But I want to bundle my .html file (like the .png file) in my jar file in order to use getResource
Unfortunately I don't see how to proceed

Can you please provide some guidance to do that ?
I'm really stuck here.
Thanks in advance

Certainly I'm searching on the wrong place
I don't know what is wrong in my (Eclipse) "java build path" where all the files under /src should be included
Again why it is working with .png and not with .htlm (even if I might understand that eclipse will automatically bundle .png)

Again many thanks for your patience and help for a clear explanation and guidance

Merci
 
Dave Tolls
Ranch Foreman
Posts: 3056
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're reading the resource, so use getResourceAsStream and use InputStreamReader instead of FileReader.

Norm's right.  A resource in a jar file is not a File, so you can't treat it as one.
 
Christian Klugesherz
Ranch Hand
Posts: 103
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Many, many many thanks to all.

Understood, and fixed.



Topic closed.

Merci !
 
Rob Spoor
Sheriff
Posts: 21131
87
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:A resource in a jar file is not a File, so you can't treat it as one.

Interesting fact - you can treat it as a Path (from NIO.2).
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!