Win a copy of Event Streams in Action this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Platform Dependent Jar Path Problem

 
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've written an application that works on both Windows and Linux with one exception. I use the following string for a directory path:

I then use that to instantiate a buffered stream thus:

Running the jar on Windows it finds the file but running on Linux I get a file not found exception. Obviously there is a difference in the way the path string needs to be formatted for Linux but I do not know what that is. I can determine the OS and then customize the path as necessary but I need someone to tell me the correct syntax for a Linux path. TIA.
 
Rancher
Posts: 3410
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at the API doc for the File class.  It has some static values for building paths on different OSs
 
Sam Ritter
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks but I'm not sure I understand. The docs seem to refer to files outside of the jar. I'm having trouble with the path internal to the jar. An I looking at the wrong docs?
 
Norm Radder
Rancher
Posts: 3410
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I might have misread the question.  I don't know why there would be a difference between the two OSs.  
Are you sure all the files in question are in the jar file?  Does the jar file work at different locations on the Windows system?
 
Norm Radder
Rancher
Posts: 3410
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
>>> Duplicate <<<
Content minimized. Click to view
 
Sheriff
Posts: 24594
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Sam Ritter wrote:Obviously...



On the other hand, I notice that you are using a relative path. So the hypothesis that your working directory isn't what you think it is, that's also a possibility. A fairly strong one, too, given that Unix does use the / as the path separator.
 
Sam Ritter
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good point. Perhaps Linux requires the leading / while Windows does not. I'll have to test that. Thanks.
 
Norm Radder
Rancher
Posts: 3410
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

while Windows does not.


If the files are all in the jar file, accessing the contents of the jar file with getResource should work the same on any OS.
If not, Won't that break the contract: compile once, run anywhere?
 
Paul Clapham
Sheriff
Posts: 24594
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norm Radder wrote:If the files are all in the jar file, accessing the contents of the jar file with getResource should work the same on any OS.



Yes, it should. But that's assuming that the resource in question is really being found in the jar on the Windows machine, which appears to work correctly. The code which was posted just looks in the classpath, so perhaps it's accidentally finding it somewhere else on Windows.

I also didn't think you got a FileNotFoundException when the getResource() method didn't find the resource, but I could be wrong about that. I haven't written a little test program to see whether that's the case or not.
 
Norm Radder
Rancher
Posts: 3410
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think we need more information.
@Sam Ritter  Can you copy the full text of the error message and paste it here?
Also post the code that is relevant to the error.
 
Saloon Keeper
Posts: 10415
223
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aye. If the resource is really inside the JAR, the only reason it might work on one system and not on the other is if the path used doesn't actually correctly point to the resource in the JAR, and by coincidence DOES point to a file that happens to be on the class-path on one of the two systems.

We need to know what directory the resource is in, relative to the root of the JAR, and what package the class is in that needs access to the resource.
 
Sam Ritter
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, so the relative path works for Windows only and adding the leading / (presumably the absolute path) does not work for Windows or Linux.

The root level of the jar contains:
com
META_INF
org
stokerMonitor

'stokerMonitor' contains the 'audio' sub-directory and all the class files including 'AlertDialog.class' that contains the buffered input stream trying to access audio/filename.

I guess the question is should the path in 'AlertsDialog.class' be absolute with respect to root, in which case '/stokerMonitor/audio' should be correct in all cases. Or relative to AlertDialog.class in which case 'stokerMonitor/audio' should never work but rather just 'audio' as a relative path. Which I think I tried at one point and did not work but I may need to retest that.
 
Norm Radder
Rancher
Posts: 3410
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you copy the full text of the error message and paste it here?
Also post the code that is relevant to the error.
 
Sam Ritter
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We may be on to something here. I retested 'audio/' as the relative path on Windows. It did not work but I got a difference exception, it was a stream closed exception. That must be why I changed the path to 'stokerMonitor/audio' which does work at least for Windows.
 
Sam Ritter
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm: Which path do you want?
 
Norm Radder
Rancher
Posts: 3410
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your first post You said:


I get a file not found exception


Can you copy the full text of that error message and paste it here.

Also the error message will have a source code line number for where the exception happened,
can you copy the code around that line number and paste it here
 
Sam Ritter
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arrgh!! I cannot reproduce the error any more. I keep getting the stream closed exception now other than when I use 'stokerMonitor/audio'. I need to see what that exception means and play around with it some more.
 
Norm Radder
Rancher
Posts: 3410
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you post the full text of that error message
and the statements around where it is happening?
 
Sam Ritter
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just for grins and giggles I changed the sub-directory to 'resources' since that seems to be  pretty common usage. As expected it didn't matter. In any case this is the code that is trying to access the file:

Since JOptionPane does not allow copying the text I have attached an image. If you need a full traceback I will have to change the JOptionPane code to do that.
exception.png
[Thumbnail for exception.png]
Exception Screen Shot
 
Norm Radder
Rancher
Posts: 3410
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Add a call to the printStackTrace method in the catch block to show the full text of the error message.
Then copy the full text and paste it here.

The message will show the method that was called and the source line number.
 
Sam Ritter
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Line 143 is this one in the above code:

exception.png
[Thumbnail for exception.png]
TraceBacjk
 
Sam Ritter
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see "unknown source". Is that the same as file not found?
 
Norm Radder
Rancher
Posts: 3410
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I see "unknown source". Is that the same as file not found?


I think has to do with the debug info saved with the class file in question.

Note: Posting an image instead of text makes it hard to copy text from the error message to use in  a search.
 
Sam Ritter
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know. Is here a type of display window that will allow copying text since JOptionPane won't? I did a search on stream closed exception but didn't find anything applicable.
 
Norm Radder
Rancher
Posts: 3410
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I get the same error when the file is not found.
Print out the value:  System.out.println("stream="+this.getClass().getClassLoader().getResourceAsStream(resourcesPath+path_));
to see if it is null or not.  If null, the file is not being found.
 
Sam Ritter
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The plot thickens. I found a CentOS 7 system to run on. Both that and Windows works. The requested print yielded the first screen shot.

However, the Linux that I was originally using where I was having the problem was Rasperian. Now I am getting something different and I don't know what it means. The 2nd screen shot.
windows.png
[Thumbnail for windows.png]
Windows and CentOS
2019-07-19-141107_1920x1080_scrot.png
[Thumbnail for 2019-07-19-141107_1920x1080_scrot.png]
Raspberian
 
Sam Ritter
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looking up that error message it may have something to do with the bit rate???
 
Saloon Keeper
Posts: 2658
333
Android Eclipse IDE Angular Framework MySQL Database TypeScript Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What was the message for the LineUnavailableException?
 
Sam Ritter
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure what you are asking for. Everything after the hypen (-) is e.toString. That is all the message there is. Are you looking for the traceback?
 
Ron McLeod
Saloon Keeper
Posts: 2658
333
Android Eclipse IDE Angular Framework MySQL Database TypeScript Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Sam Ritter wrote:Not sure what you are asking for. Everything after the hypen (-) is e.toString. That is all the message there is. Are you looking for the traceback?


I was looking for what would be returned by e.getMessage() - it seems like it is included in returned by toString() if a message is present.

I'm a bit surprised that there is no message set for the exception - I was expecting there would be something like this:


The message may come from the underlying audio system implementation - maybe there is no recognizable audio system on the platform where you are running your app??
 
Ron McLeod
Saloon Keeper
Posts: 2658
333
Android Eclipse IDE Angular Framework MySQL Database TypeScript Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try printing-out the audio resources on the platform:
 
Sam Ritter
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I did that and found that the 2nd mixer, rather than the default works. My next problem is to determine the specific OS in order to use that just for the Pi. When I get the OS from System.getProperties("os.name") it just returns 'Linux'. That is not sufficient to determine if it is Raspbarian rather than some other distro.
 
Sam Ritter
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This may not really be a java issue any more. I'm thinking the better solution is to figure out how to change the Pi configuration to make the correct mixer the default.
 
Ron McLeod
Saloon Keeper
Posts: 2658
333
Android Eclipse IDE Angular Framework MySQL Database TypeScript Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Sam Ritter wrote:When I get the OS from System.getProperties("os.name") it just returns 'Linux'.


Take a look at the other os System Properties.  I don't have a Pi in front of me, but this is what I see with Centos 7:
 
Sam Ritter
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. I'll see if those help.
 
Bartender
Posts: 20928
127
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java has a universal (portable path notation), which is Unix-style (forward slashes). You can (and should!) use this format in your Java code. Partly because it helps make your apps more "write once/run anywhere" and partly because those   backslashes that Windows uses can really bite. Windows itslef will permit forward-slash notation in certain cases, but unfortunately, I don't have a hard-and-fast guide to when and where.

Absolute paths are preferable to relative paths, since you don't have to worry about whether you're currently working in the right directory or not.

Note also that there are filesystem paths and resource paths, and while the two look very much alike, they operate very differently. A filesystem path is something that you can use at the command line and in file open system calls. A resource path is, for example, the location of a "file" or "directory" within a ZIP (jar, war, etc.) file. It's also the path part of a URL, where a URL like http://localhost/mywebapp/foo" might refer to either a WAR resource named "/foo" or be translated as the logical name of a servlet, resulting in the access of WAR resource /WEB-INF/classes/com/coderanch/wierdservlets/foo.class, depending on how the webapp was configured (servlet mapping in web.xml).
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!