Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CSV file for DryMode not found when executing from .jar #843

Closed
sebasira opened this issue Dec 4, 2021 · 14 comments
Closed

CSV file for DryMode not found when executing from .jar #843

sebasira opened this issue Dec 4, 2021 · 14 comments
Assignees
Labels
enhancement New feature or request help wanted Extra attention is needed
Milestone

Comments

@sebasira
Copy link

sebasira commented Dec 4, 2021

Release number
v5.0.6

Describe the bug
I've created a .jar file and when executing it like

java -jar test.jar

There's a log entry saying that the user-trade.csv file can not be found

2021-12-04 21:58:43.194 ERROR 26366 --- [           main] t.c.t.b.util.dry.UserServiceDryModeAOP   : user-trade.csv not found !

I have no problem when debugging from the IDE. What's strange is that the file is present inside the .jar and I've also put it outside the .jar, in the same directoy

To Reproduce
I can only reproduce it when running the external jar

Expected behavior
I would expect the file to be picked up and the fake account will be configured

Screenshots
Here you can see the CSV inside the .jar
Screen Shot 2021-12-04 at 19 04 20

@straumat straumat self-assigned this Dec 5, 2021
@straumat straumat added the bug Something isn't working label Dec 5, 2021
@straumat straumat added this to the 5.0.7 milestone Dec 5, 2021
@straumat
Copy link
Member

straumat commented Dec 5, 2021

@sebasira Hi ! why do you want to make a jar with those kinds of files included ?

@straumat straumat added the waiting Waiting for more information label Dec 5, 2021
@sebasira
Copy link
Author

sebasira commented Dec 5, 2021

@sebasira Hi ! why do you want to make a jar with those kinds of files included ?

I don't! I just want to make a .jar and test the dry mode in a cloud server. The problem is that the compiled .jar does not pickup the .csv so I try leaving it inside the jar (this setup is the one I have when debugging from the IDE, I have the .csv in the resource folder).

I also try with the .csv file outside the .jar next to it in the same directory but still it won't be picked up.

I'm thinking the file path could be the problem when loading the file with Scanner

@straumat
Copy link
Member

straumat commented Dec 7, 2021

@sebasira indeed, when making a jar, the files in src/test are not taken. Can you move your csv files to src/main/resources ?

@sebasira
Copy link
Author

sebasira commented Dec 7, 2021

They are already at src/main/resources.

From the screenshot of the first post, you can see that the the generate jar has the CSV

@sebasira
Copy link
Author

sebasira commented Dec 7, 2021

I've try to add a log entry at the point where Scanner try to pick the file up, so I can log the file path, but I'm having trouble compiling the project. I'm used to gradle rather than maven.

@straumat straumat removed the waiting Waiting for more information label Dec 7, 2021
@straumat
Copy link
Member

straumat commented Dec 8, 2021

@sebasira you can build and isntall cassandre release by typing: mvn install -Dgpg.skip -DskipTests

@straumat straumat added the waiting Waiting for more information label Dec 8, 2021
@straumat
Copy link
Member

@sebasira can you tell me the command you used to create the jar ? Thx

@sebasira
Copy link
Author

I'm sorry I do not know much about Maven and after executing the command you proposed:

mvn install -Dgpg.skip -DskipTests

I add it to my project and all I get is an empty .jar

I can not build the project to test and work on it.

See:
Screen Shot 2021-12-15 at 23 21 29

@straumat
Copy link
Member

@sebasira i'm sorry. My question was, what command did you use to build your test.jar ?

@sebasira
Copy link
Author

Oh I see!

I've built my app with gradle wrapper using

./gradlew build

@straumat
Copy link
Member

@sebasira did you try with this https://stackoverflow.com/a/52404325

@sebasira
Copy link
Author

I manage to build this project (maven) and use the SNAPSHOT version of Cassandre in my test app. I'll try with a log entry where Scanner looks for the file and get back in touch

@sebasira
Copy link
Author

sebasira commented Dec 20, 2021

Well... I'm back...


This entire comment is about this line


After a lot and a lot and a lot of testing here's what I've found out:

1- I couldn't find (yet) a method that works for both: file in the resource folder and file outside the .jar, next to it.

2- The actual code:

Scanner scanner = new Scanner(file.getFile())

only works (at least for me) when debugging the app. I mean run it from the IDE (IntelliJ)

3- Replacing it with:

Scanner scanner = new Scanner(file.getInputStream())

works with the complied .jar but only looks for the file user-trade.csv inside the resource folder within the jar

4- Using this other line:

Scanner scanner = new Scanner(new FileReader(file.getFilename()))

Does not load the file inside the resource folder, but read the outer file, sitting next to the .jar file

At this point my idea is to:

  • Try to load the file from outside the .jar according to (4). If that fails because there's no file outside the .jar try looking for it inside the .jar, according to (3). If that fails, then we can assure there's no file to be found

@straumat straumat removed the waiting Waiting for more information label Dec 20, 2021
@straumat straumat added enhancement New feature or request and removed bug Something isn't working labels Jan 1, 2022
@straumat straumat modified the milestones: 5.0.7, 5.0.8 Jan 1, 2022
@straumat
Copy link
Member

straumat commented Jan 1, 2022

@sebasira don't you think the problem come from this method:

    private List<Resource> getFilesToLoad() {
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        try {
            final Resource[] resources = resolver.getResources("classpath*:" + USER_FILE_PREFIX + "*" + USER_FILE_SUFFIX);
            return Arrays.asList(resources);
        } catch (IOException e) {
            logger.error("UserServiceDryModeAOP encountered an error: " + e.getMessage());
        }
        return Collections.emptyList();
    }

@straumat straumat added the help wanted Extra attention is needed label Jan 6, 2022
@straumat straumat closed this as completed Jan 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants