Win a copy of Head First Go this week in the Go 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
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Devaka Cooray
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Tim Holloway
  • Claude Moore
  • Stephan van Hulst
Bartenders:
  • Winston Gutkowski
  • Carey Brown
  • Frits Walraven

Jar file path question  RSS feed

 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm having difficulty setting up the path to my 'resources' folder. It is because I don't understand how paths work once a runnable jar has been created. Specifically what is the 'root' direcctory. The top level of directories are:

com
META-INF
org
src
StokerMonitor

The last directory contains all the class files including 'main' so I am guessing that is the 'root' directory when running. In the 'src' directory there is a 'resources' directory which is where I have files I need to access. Since the directory I want is above the 'root' directory, I don't know how to reference that path in my source.

I suspect it is related to how the directory structure was set up in Eclipse but I don't know which I should be correcting or how. The Eclipse tree is:
 
Sheriff
Posts: 5750
149
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My understanding is that the Maven defaults for resource directories are /src/main/resources and /src/test/resources.  I'd try those first.
 
Sam Ritter
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for not being clear. My src directory does not have a main sub-directory in it. The issue not the Maven or any dependencies. Rather it is the path I specify in the source code to open files in my resources directory. What I have works fine in Eclipse when I run the app. The problem is, it cannot find those files when I create the jar and run that. The directory structures are very different.
 
Saloon Keeper
Posts: 9869
199
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How are you trying to access the resources? And how have you specified your resource folder?

Please show the relevant code and your POM.
 
Saloon Keeper
Posts: 20514
115
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maven doesn't like that organization.

It expects the source code to be in src/main/java and the resources to be in src/main/resources and it doesn't care what Eclipse thinks.

When Maven builds a JAR component, the src/main/resources directory components are copied to the root level of the resulting JAR.You can confirm this by doing a directory listing of the JAR with a command in this format: "jar -tvf myjar.jar".
 
Sam Ritter
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the replies. Again, Maven is not the issue per se. The app works except it cannot find the files I try to open that are in the resources folder. The crux of the problem is the path to that folder. This is the path that works when I run it from Eclipse but fails when I export the jar and run that.

This is the code that accesses the files:

If your suggestion of reorganizing the tree to make Maven happy will fix this problem too, then I will try it. I just want to make sure we are all addressing the same problem.
 
Knute Snortum
Sheriff
Posts: 5750
149
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are using Maven to build the executable jar file, then Maven is the issue.  If so, could you post your pom.xml file?  Or just try moving your /src/main/resource.  Then your resourcesPath string can be set to "".
 
Tim Holloway
Saloon Keeper
Posts: 20514
115
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You do need to arrange your directories in Maven-approved format. Eclipse will happily allow itself to be adjusted for that.

When Maven builds the JAR, it copies the CONTENTS of the src/main/resources directory, so unless you place your resources in a sub-directory, the resourcesPath would be "", not "resources/".

Also, since Maven builds into a project subdirectory named "target" (which it will create, if needed). the app should consider the file structure of the target directory when testing, not the structure of the sources directories. When Maven executes the jar:jar goal, it will zip up the resources of the target directory's artefact sub-directory and create the jar for that artefact in the target directory, which is where it will in turn copy it to the local Maven cache if so instructed.
 
Sam Ritter
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to make sure I understand correctly, the directory tree should be changed to this:
 
Tim Holloway
Saloon Keeper
Posts: 20514
115
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yep.

The src/test/java and src/test/resources directories would be created to hold stuff for unit-testing. If you needed them.
 
Sam Ritter
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I'm not sure what I did wrong but I think I have a mess now. After creating the folders and moving stuff Eclipse seems to have its own ideas. My current directory tree now looks like this in package explorer.

So Eclipse changed from folders to main.java and main.resources. Maybe that is correct but what do I need to do to fix the project statements? I have a ton of other errors but I think that some refactoring needs to be done once I get the project fixed.
 
Tim Holloway
Saloon Keeper
Posts: 20514
115
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You use the Eclipse Project Properties dialog to change the source path to src/main/java. Eclipse shouldn't care about the resources.

Actually, if you have a maven project that isn't in Eclipse to begin with and you want to "eclipse-ify" it, the "mvn eclipse:eclipse" goal creates a proper ".project" file for Eclipse in the Maven project folder. If you get really desperate, that's a possible solution, but for an existing Eclipse project, you'd have to go through several steps to do it.
 
Sam Ritter
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is confusing. Apparently the package name must match the directory name. Thus the tree must be src/StokerMonitor.java rather than src/main.java for the project name to be correct. In any case I don't see anything in project->properties that lets me change the project name. But if I do then I will be back to where I started in non-compliance with Maven standards.
 
Tim Holloway
Saloon Keeper
Posts: 20514
115
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Sam Ritter wrote:This is confusing. Apparently the package name must match the directory name. Thus the tree must be src/StokerMonitor.java rather than src/main.java for the project name to be correct. In any case I don't see anything in project->properties that lets me change the project name. But if I do then I will be back to where I started in non-compliance with Maven standards.



Not so. The source path MUST be src/main/java. But you must configure Eclipse properly.

Right-click on the project in the Java or Package explorer tree view to bring up the project dialog. Select the "Java Build Path" option from the dialog's tree view. Use the "Add Folder" and "Remove" buttons to change the Eclipse source code directory settings.

Check the "Allow output folder for source folders" control and set the Default output folder to (project directory name)/target/classes

You should now have Eclipse in sync with Maven.

The Project is a construct in the Eclipse workspace. Its name can be changed by right-clicking it and selecting Rename in the Package Explorer tree. But you don't need to do that.
 
Sam Ritter
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry if I'm dense but I don't see any project dialog in the contextual menu. It is a long list but properties is not the right place and neither is build path -> configure build path ...

I guess I don't know what I'm looking for.
 
Tim Holloway
Saloon Keeper
Posts: 20514
115
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From Eclipse GUI:

On the titlebar Menu: Window/Show View... Select (Other) Java/Package Explorer. This should open an Eclipse View pane with all the projects in your workspace.

right-click on the project name in the Package Explorer view. The context menu that pops up should include Properties (Alt-Enter) as the last item on that menu. Select it to bring up the Project Properties dialog.

The Project Properties dialog has a control on its left-hand side that presents a tree of different types of project properties.The Java Build Path is the one of primary interest here.
 
Sam Ritter
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wish there was a way to add a screen shot on this forum. I thought you were referring to something different. I already had done that but that made things even more confusing so I undid it. Anyway, I redid it and now have this structure:
 
Marshal
Posts: 63496
207
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can add screenshots, but they are often difficult to read, so we discourage them.
 
Tim Holloway
Saloon Keeper
Posts: 20514
115
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:You can add screenshots, but they are often difficult to read, so we discourage them.



But since you asked....
projectproperties.png
[Thumbnail for projectproperties.png]
Eclipse Project Properties Dialog
 
Sam Ritter
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just noticed the attachments tab. Here is what I have at the moment (maybe this is becoming more of an Eclipse question now):

screenshot.png
[Thumbnail for screenshot.png]
Eclipse Project Explorer
 
Sam Ritter
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see what you mean about being hard to read. Unfortunately preview with an attachment does not work and there is no way to edit a post. I'll try again by zooming in on the important part.
screenshot.png
[Thumbnail for screenshot.png]
 
Tim Holloway
Saloon Keeper
Posts: 20514
115
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To get the Project Properties Dialog, right-click on your project name in that treeview (StokerMonitor). A long context menu should appear, and the bottom-most item on that menu should be Properties (Alt-Enter).
 
Tim Holloway
Saloon Keeper
Posts: 20514
115
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, just as a note, use of the Default package is not encouraged. Some Java tools will whine about that. It's best if you assign your classes to a named package.
 
Sam Ritter
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We're confusing the issue. I know how to get to properties from the contextual menu but where do I go from there?

It is the new directory tree that is the issue. That not happen until I tried to rearrange it to conform with your suggestion to make Maven happy and get the path corrected for my resources file. I did not do it that way, Eclipse did. I see no way to rename the "default package" folder. That is the crux of the problem as all my classes get an error on the package statements.
 
Tim Holloway
Saloon Keeper
Posts: 20514
115
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, if you have a setup like this:



Probably the best thing would be to shut down Eclipse, use your command-line or File explorer to create a folder named "stokerMonitor" under src/main/java, and move StokerMonitor.java into it. Then restart Eclipse.

You can fix it inside Eclipse, but this way is probably going to be easier for you.

 
Sam Ritter
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is already the structure in file explorer.
 
Stephan van Hulst
Saloon Keeper
Posts: 9869
199
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you show us what Eclipse looks like when you expand the *entire* tree in the package explorer?

What happens when you run the command mvn package in the root of your project folder?
 
Tim Holloway
Saloon Keeper
Posts: 20514
115
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Sam Ritter wrote:That is already the structure in file explorer.



Try pressing F5 and see if that causes it to clean itself up.

There are ways to bring Eclipse back into line when the package path and the file path are out of sync, but unfortunately I don't remember them. You'd have to ask in the Eclipse forum.
 
Sam Ritter
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't have 'mvn' installed. I let Eclipse handle that on export.
pt1.jpg
[Thumbnail for pt1.jpg]
pt2.jpg
[Thumbnail for pt2.jpg]
 
Stephan van Hulst
Saloon Keeper
Posts: 9869
199
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the default package in Eclipse, make a new package called stokerMonitor, and then move all the files that are currently in the default package into that package. At the very least, that should bring Eclipse's view of the world in line with what's on your file system.

I also strongly recommend installing Maven and compiling a few projects manually, so you can get a hang of it without an IDE getting in your way.
 
Sam Ritter
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just so I understand you:
 
Stephan van Hulst
Saloon Keeper
Posts: 9869
199
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. However, as soon as you have created the new package and moved all source files there, the (default package) node should disappear.
 
Stephan van Hulst
Saloon Keeper
Posts: 9869
199
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It might also be that before you move the source files, Eclipse shows the packages side by side, like this:

I'm not sure. I don't work much with Eclipse. I don't like it.
 
Sam Ritter
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That brought me back full circle. I now have the same tree as I did when I started this thread. I guess rather than try to make Maven happy, which is not a problem as it is, I need to figure out the correct path to resources to use in my code.
 
Stephan van Hulst
Saloon Keeper
Posts: 9869
199
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:How are you trying to access the resources?

[...]

Please show the relevant code and your POM.

 
Stephan van Hulst
Saloon Keeper
Posts: 9869
199
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you give an example of a *specific* path to a soundclip that you want to play? For instance, when it fails to load the sound clip, can you post the exact error message that your dialog displays? Can you also show is the *exact* location of the sound clip inside the JAR after you unpack it?
 
Sam Ritter
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My 2nd post shows the specifics and the code. I don't understand why Maven keeps coming into this. That really was an aside suggested earlier. The problematic code is pure Java.

As for the jar, my OP shows the structure of the jar. The clips are in the 'resources' folder while the classes are in the stokerMonitor folder. As I said in the OP I just need to understand hoo to reference that path in my code.
 
Stephan van Hulst
Saloon Keeper
Posts: 9869
199
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's entirely false. Unless you have a src/resources folder inside your src/main/resources folder, there's no way you can have a src/resources folder in the root of your JAR unless you have either modified your POM some way, or you are not using Maven to build.

Something is off. Either your directory structure is not as you wrote in the first post, or your POM is unconventional, or you are not using Maven to build a Maven project.

In any case, all bets are off on us trying to help you find the right path to use.

Please post your POM. It would also help of you could attach your Eclipse project file.
 
Stephan van Hulst
Saloon Keeper
Posts: 9869
199
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After some experiments it's most likely that even though you're using a Maven Project Nature AND you have a Maven POM in your project, you're not using Maven to build and run the project, and you also have not configured the resource folder in Eclipse as as source folder.

Why do you have a Maven POM if you're not using Maven?

Either way, you must add the resource folder as a source folder to Eclipse. Attached is what you want the build path to look like.
build-path.png
[Thumbnail for build-path.png]
 
Sam Ritter
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bingo. Moving the resources folder into the same folder as the source was the key.

As an aside, I am indeed using Maven (itextpdf) and that was never part of the problem. While I may not have the structure Maven compliant, it works and my philosophy is to not fix something that is not broke.

Thanks for all the help and patience.
 
money grubbing section goes here:
Become a Java guru with IntelliJ IDEA
https://www.jetbrains.com/idea/
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!