How to make file from dependency JAR accessible during command line execution? - java

I have 2 projects:
Project A has folder/file.ext in src/main/resources. When built, it is present in a.jar.
Project B is referencing folder/file.ext somewhere in the code.
I can not change project A or B or how they're built.
When running project B from the command line with:
java -cp a.jar com.mycompany.mygroup.MainClass
I am getting a FileNotFoundException.
I'd like to know if there's a way to make this work, where folder/file.ext is made visible/available to the main class of project B without having to build a fat jar or anything like that.
Thanks.

Try adding another source folder in Project A, with the contents of Project B.
Not really sure why you would need two different projects, instead of adding all the code in one project with different packages/folders.

Related

How to read file from file system during Maven build of module

I have a Maven project A (packaged as a pom) containing Maven module project B (packaged as a jar).
Project B is also physically in the root of project A.
When B is being built, a plugin indirectly uses my code (inside B) to access a property file in its src/main/resources location.
When I build project B directly (mvn clean install) the code can easily find that file by using new File("src/main/resources/foo.properties");
However when I try to build project A, it first will try to build the module B, and in that case it cannot find the property file.
Apart from the 'new File' variant, I have tried using
this.getClass().getClassLoader().getResourceAsStream("src/main/resources/foo.properties");
and I tried using Spring:
Resource resource = new ClassPathResource("src/main/resources/foo.properties");
Both also with a "/" prefix. But the file simply cannot be found.
Why is that? Might it be looking for the file in the root of A? Is it possible to find the file in module B when building A?
Thanks!
new File("src/main/resources/foo.properties");
This is a path relative to the working directory. The working directory is wherever you called maven. So that's why it won't work when calling maven anywhere other than the directory of module B.
this.getClass().getClassLoader().getResourceAsStream("src/main/resources/foo.properties");
This won't work because the classpath is where maven put the classes, that is, target/classes.
What you need to do is add this file as resource in the maven build, then use the getResourceAsStream idea with the correct path. Since you used the standard maven layout, you probably don't need to anything and just use
this.getClass().getClassLoader().getResourceAsStream("foo.properties");

How to make Java package with classes

I would like to make a Java package in a JAR file with precompiled classes such that other Java projects can consume these. How do I do that? It seems to me that most guides I have found expects a Main class/method to be available, but I do not want this to be an application that runs by itself. Furthermore, the resources (various files) inside of my project should be put into the JAR, since my app depends on these. Is this possible? I am (by the way) using Gradle.
A claim has been made that this question is a duplicate of this: Java creating .jar file. However, this question assumes the existence of main methods, and it does not concern how to include resources.
You can create the jar from the command prompt.
Copy all the classes that you want to include into a folder.
Then open that folder in command prompt and issue this command.
jar cfv YourProjectName.jar *
And a JAR will be created in the same folder containing all the classes.
Another solution:
If you are using eclipse try:
Right Click on the Package -> Export -> java -> jar file
You could also select the Classes and right click on them instead of the Package.
Edit:
Refer to https://docs.oracle.com/javase/tutorial/deployment/jar/build.html for more details on this command.
you can put all your methods/functions in class file then export it to .jar
then add the jar to your project's build path. Now you should be able to call those functions from your current main java class.

Accessing files in a project that is on the build path of another

I'm working on a framework-type project in Eclipse, that has a res folder attached with, as you guessed Resources inside of it. At the moment, they are crucial text-files that are to be read.
I also have a main test rig that is apart of it's own Eclipse project, as I plan to expand it in the future. This test rig has included the framework project on it's Build Path, so I can access classes from the framework as they are added.
But this is where my problems lie: If I want to import a class, that works fine. If I want to access a file through an InputStream, or Reader, it breaks. I know this is because the files included from the secondary project aren't placed inside the test rigs bin folder, rather they reside in their own bin in the other project.
Is there a way I can get my main project to access resources from the other without having to do ../../ trickery in my file paths? I plan on making this a real thing and don't want file paths doing things like that.
In eclipse do this,
Build Path ---> Configure Path ----> Click Project Tab ---> Add the project.
Also, you can package the other project in a jar and import it as an external jar.

Can't access resources from same directory in several projects with Class.getResource in eclipse

I use maven to create a runnable jar from multiple projects :
Project A (contains the Main class):
/src/main/resources/META-INF/resources/a.txt
Project B (depends on Project A):
/src/main/resources/META-INF/resources/b.txt
Runnable.jar (create with mvn):
/src/main/resources/META-INF/resources/a.txt,/src/main/resources/META-INF/resources/b.txt
In the Main class I use Class.class.getResource to get the content of a.txt and b.txt.
When I do java -jar Runnable.jar, I can access to a.txt and b.txt. But if I run it from Eclipse, I can only access to a.txt . Is it possible to get it right? I'd like to change resources content when the application is running and get the result without recompiling.
The Run configuration :
[MAIN] => Project : Poject B, Main class : Main
[ClassPath] => UserEntry : Project A, Project B
NB : If I change the order of the UserEntry, I can access to b.txt but not a.txt.
Could you help me?
Project B needs to be added to Project A's Java Build Path. Open up project A's Properties dialog and do so.
I've found a solution. I've created a linked resource folder in project B (B/src/main/resources-a) which refers to the resources directory of project A (A/src/main/resources). I set the new linked folder as a source directory. Now it works and I can see the resources contained in the Project A in the target/classes folder of Project B. It's exactly what I expected but if I'd like 'mvn eclipse:eclipse' automaticaly configure .project and .classpath files?

getting error while importing .class files in a project in eclipse

I made a java project. It has dependencies. I have the class files. So what i did i made a classes folder and put all the class files in that folder and then add that folder to my project. All the dependencies have been removed. But i think class files also have anonymous classes. SO i am getting error at that line. Here is my project structure
The error that i am getting is
The project was not built since its build path is incomplete. Cannot find the class file for com.suprema.ufe33.UFScannerClass$UFS_SCANNER_PROC. Fix the build path then try building this project
The type com.suprema.ufe33.UFScannerClass$UFS_SCANNER_PROC cannot be resolved. It is indirectly referenced from required .class files
How can i solve this error ?
Thanks
I think the problem is that the folder structure you created in the classes/ folder does not exactly match the package name of the classes you are referencing. Check the capitalization of ufe33.
By the way, it's usually better/easier to use a JAR of such dependencies instead of folders of .class files.
I solved the issue. Yes E-Riz is right that folder structure is not right. It should be small ufe33. Also i created the jar. What i did is I ran this command where i copy all the four class files.
D:\jars>jar cf myjar.jar com\suprema\ufe33*.class
It made the jar file myjar.jar. Then i simply add this jar to my eclipse project class path and the problem solved.

Categories