How can my project access its "resources" directory both when run in Eclipse and from a Maven-packaged jar file? - eclipse

I'm working with Maven project in Eclipse (with help of m2e plugin). When I pack my project into jar-file (mvn install), all files from "resources" are located in the root of jar.
Therefore, in my program I should use only bare file names:
File file = new File("foo.txt");
But when I build and run my project by Eclipse, I would have to use the relative path to the file:
File file = new File("src/main/resources/foo.txt");
What should I do to solve this problem?

To access your program's resources, don't use File, FileInputStream and similar classes.
They will not work for anything inside a jar file.
Instead, use Foo.class.getResource(...) or .getResourceAsStream() to access your resources. (Read the documentation before doing so.)
I'm not sure if a program started from eclipse can access these - please try and report back!

Your package configuration in Eclipse is wrong, cause it sees src/main/resources as a package instead of a source folder.
The configuration in Eclipse must look like this:

Related

NetBeans Include External JAR in Export to Zip

I have a NetBeans project that uses the GSON library. I've tried including the GSON.jar file without requiring future users to separately download it. However it doesn't seem to work. The project looks for the file from the relative path of my computer so the file isn't found on another user's computer. Is there a way to include GSON.jar and "Export to Zip" and keep the reference in the project itself? I'm lost!
Thank you
Exporting a Project to ZIP zips up the project folder only, and not anything outside of the folder, including dependencies. If you include the GSON.jar file in the project folder, then the JAR file will be included in the .ZIP file. It's a good practice anyway since NetBeans will use a relative classpath and thus if you move the project itself NetBeans won't give you an error message when loading the project.

JBoss 6.1.1.EAP Module Deployment: Class not found

The structure is as follows:
Actually deployed module is EAR.
EAR contains WAR module, and WAR module contains another JAR module.
In this last JAR, there are some generated classes and their parent folder is also used as source folder. Its path is "target/generated-sources/java".
The problem that is killing me, is that the deployed application throws ClassNotFoundException on server start, and the classes in question are the generated ones.
Now the trick:
if I explicitly change the Deployment Assembly in WAR project in Eclipse not to contain JAR project as "project", but as an archive from the JAR's project "target" folder, JBoss sees the generated classes and starts.
This solution however works only until next eclipse maven project update, so manual edit of deployment assembly is not really a solution.
Any ideas how to deploy or reorganise packages correctly?
OK, seems I have found the solution.
I looked through the build-path of the mentioned JAR file and saw that the entry Output folder pointed to target/test-classes.
After changing this entry to target/classes the application deploys and starts without any missing generated classes.
This is what worked for me:
Expand the 'target' folder of your maven project inside Eclipse (Project Explorer View);
Refresh it (F5);
Right click on your project on 'Servers' tab, then select "Full Publish";
Start your JBoss.
I had to enforce the JBoss Tool "Full Publish" to get an updated version of my target folder by Refreshing it manually on eclipse.
I don't know why but sometimes the Publishing from maven projects (even Full Publishes) do not copy classes from the target Project as it is in the file system. Maybe it's using some outdated memory info or some cache...
Anyway, this is what works for me.
After an update to Eclipse 4.15 and JBoss tools I got this problem too.
My solution: project -> properties -> Java Build Path --> Source
There my Output folder from my source was linked to project/target/classes, I changed this to project/target/project-projectversion/WEB-INF/classes
When I looked into the standalone JBoss folder I saw the folder structure of my source, but the classes where missing, when I changed the output folder the classes pop up and everything worked like before.
I am pretty sure the update made the problems.

deploy jar Java Desktop Application in jdeveloper12c

I have always developed my java NetBeans projects. When I finish my project, and automatically build "dist" folder where the main jar and a "lib" folder containing all the external jar was created. This is done automatically with the NetBeans IDE
Now I'm trying to do a project with jdeveloper 12c Java Desktop Application and try to deploy my project but I have problems with java.lang.NoClassDefFoundError. I meet several questions:
Can you add the same structure as I said before? That is, a file jar and inside a folder "lib" place the jars using How to do it?
I've seen examples that by New File Group adds refencias, but it really adds to the main jar file (as I see in the size of the file) but not saved in a separate folder.
I do not know if you can help me do this in jdeveloper 12c: Generate a jar file and folder lib with references.
Thank you for your help.
Go into the deployment profile for your project and add all the libraries and files that you need to be included to the JAR you are generating.

trying to use tess4j for the first time with eclipse(any step by step for trully beginner)

I need a step to step on how to plug tess4j to Eclipse.
I found this online:
creating a lib directory and copied the tess4j.jar and its required jar in;
added the jars to build path
copied the tessdata directory and tessdll.dll file into the project root directory.
Now it just works.
I only know basic Java so I have no idea what creating a lib directory and copy directory file means at all.
Can someone help me to plug this library and make it work for eclipse because I am working in a project that needs ocr.
creating a lib directory and copied the tess4j.jar and its required jar in;
A JAR (Java ARchive) file is a collection of compiled Java classes. This is the usual way to distribute Java libraries. You should create a new folder in your project (right click your project, "New", "Folder") and copy/move the JAR you downloaded to this newly created directory. If tess4j has dependencies, i.e. needs other JARs to work, you have to put them in this directory.
added the jars to build path
You need to tell Eclipse to add the JARs to the build path, i.e. Eclipse needs to know where to get the tess4j classes from. Locate the lib folder and select all JAR files. Right click them and select "Build Path", "Add to Build Path".
copied the tessdata directory and tessdll.dll file into the project root directory.
Should be clear ;-)

how to setup the sphinx with netbeans

i have successfully configured sphinx4 with eclipse.
for that these steps i have used.
copy my java and config files to SRC folder
all the necessary jar files (in the lib). the lib folder added to the root of the project
build those jar files (jsapi files too)
change the configuration file and give the proper path
test the java file
but in Netbeans i really dont understand how to do the proper steps. can someone help me. the jar files should be added to "Libraries" rite.
then after adding them how to build them.
in the netbeans it dont show a SRC folder. so all the java files and configuration files should go to Source Packages folder rite.
can someone help me with this. please
If you have sphinx4-1.0beta6 then you can just open the folder up as a netbeans project. Open netbeans, click open project, then navigate to the sphinx folder and open it. It is already a netbeans project file type as of this version. I assume this will work in later versions also. I have no clue why sphinx doesn't say this on their website.
Sphinx4 is a Java library available in OSS repository, so you can use it as any library.
If your project uses maven/gradle, add repository in your project configuration, then add dependency on two packages - sphinx4 core and sphinx4-data. The former is the main library, the latter is required if you want to recognize US English. Then just start writing the code.
If you want to rely on jar files, just download sphinx4-core and sphinx4-data files from the repository and add them as dependency to the project, then start writing code.
If you want to modify or develop sphinx4 itself, install Gradle support in your IDE, then import latest sphinx4 sources as a Gradle project.
For more details and links see the tutorial
http://cmusphinx.sourceforge.net/wiki/tutorialsphinx4
click on Add JAR/Folder
select jar file you want to add