Eclipse Files Not Found? Works with JCreator - java

Well I have these .java files in JCreator.
When I compile them in JCreator, it's fine, all files and folders are read and no errors occurred. But, I need to compile them in an Eclipse project to pass it to my professor. Then the problems started.
I added the .java files in a new Eclipse project and into a new package inside the
src folder
Then after that, I made a new Folder in the project called
res
and added it to the build path as a source Folder and added the files and folders in it.
>Use as Source Folder
All sub-folders are fine same name and stuff, Refreshed the project a few times, checked the build path, but when I execute the program, It says:
java.io.FileNotFoundException: sounds\bgmusic.wav (The system cannot find the path specified)
from this line:
InputStream test = new FileInputStream("sounds\bgmusic.wav");
Same folder name, same file, added it to the Build Path and stuff. Also, I repeat, It does not find ALL the files in the resource folder: Pictures, Sounds Fonts and other stuff. I already made some projects in Eclipse and I had successfully added pictures and sounds in them. But what's the problem with this?
Thanks for the help!

Try this:
ClassLoader.getResourceAsStream ("sounds\bgmusic.wav");
I hope it resolves your issue.
You can also refer below link:
http://www.javaworld.com/article/2077352/java-se/smartly-load-your-properties.html

Related

Path in Maven import being overriden by main project

I've got two Maven projects A and B. I'm packaging project A as a jar and adding it as a dependency in project B. Project A shows up in my External Libraries (using IntelliJ) and I can see all the source code and files.
In project A I've got a method that is retrieving the files in a folder located at projectB/src/main/resources/folder/, and I'm using the following code to check whether any files exist within this folder:
File folder = new File("src/main/resources/folder/");
File[] defaultDefinitions = folder.listFiles();
This works correctly when project A is ran. However, when project B calls this method, instead of appending the path to project A's working directory, it is appending it to project B's, and obviously there are no files located at projectA/src/main/resources/folder/.
How does one go about solving this issue?
The problem you're having is that you're using a relative path, and you're relying on working directories (automatically managed by the IDE). You need to read files in a way that's going to be predictable, even if you run your code outside the IDE.
You can solve the problem by using absolute paths, which is going to help even if you run the program from the command line, or anywhere outside the IDE:
String path = "/absolute/path/from/properties/etc/folder/";
//better use a program argument or properties file for this
File folder = new File(path);
Don't load Maven resources from the filesystem API :
File folder = new File("src/main/resources/folder/");
It will work only during the development phase as you have the source code besides the running application.
Instead, use the classloader :
File folder = getClass().getResource("/folder").toURI().toURL().getFile();
It will work as src/main/resources is a "special" directory for Maven as all resource files located in this directory are added in the classpath at compile time and will be also available in the classpath at runtime.
Note that you could retrieve the File from a less convoluted way by using the java.nio API :
Path folderPath = Paths.get(getClass().getResource("/folder").toURI());
Stream<Path> pathStream = Files.list(folderPath);

How to include resource folder in executable .jar file in eclipse?

I need to create an application for sorting various types of polygons using various parameters such as height, volume or base area. Arguments for Filename which has parameters for polygons, Sort type, Sort method will be pass through command line.That file is in my resource folder outside my src folder in a project. I have implemented all programs, It works fine when I run using pass arguments through eclipse run configuration. But when I try to run my .jar file using cmd same arguments it gives me FileNotFoundException.
I opened my jar file using 7zip and noticed it never extracted my resource folder in .jar file. I searched online and tried including my resource folder in to build path of eclipse. But still does't work.
Follow these steps:
1) click project -> properties -> Build Path -> Source -> Add Folder and select resources folder.
2) create your JAR!
EDIT: you can make sure your JAR contains folder by inspecting it using 7zip.
Reefer this link as well How do I add a resources folder to my Java project in Eclipse
First, you need to create a source folder for resources, for instance name it res, and then move your image folder to res. When you generate the jar file, you will see the image folder, not the res folder or files separately.
This comes down to how you are generating the JAR file.
When you're exporting the jar in eclipse make sure to checkbox the button that says "Export java sources and resources" https://www.cs.utexas.edu/~scottm/cs307/handouts/Eclipse%20Help/ensureJavaFiles.jpg
There are a lot of ways to do this one is to use Gradle is the recommended way, something like this will work Creating runnable JAR with Gradle

I added a library directly to my src folder, Eclipse seems to be compiling properly, but can't find the class file?

I added the boilerpipe library directly to my src folder. Everything seems to be compiling when I run, but I get an error telling me that one of the classes in the boilerpipe library could not be resolved.
The ArticleExtractor class is what I'm trying to use, but Eclipse won't let me even though its class file is in the bin folder.
I'd post a picture, but I don't have the reputation. Boilerpipe itself is a folder containing several other folders, containing the .java files in the source folder, and .class files in the bin folder after I've tried to run. I couldn't find anything on this here so I though I'd ask.
without seeing the code I would say that you either haven't added it to the build path or haven't imported the code into your class.

Eclipse Executable Jar Resource Files

I would like to export my Java program, but I have run into problems. It exports fine but my resource files like my res folder and my sound folder aren't there. They are located in the project directory, so I don't know if that should be a problem. I am currently just using ImageIcon().getImage() to get my pictures and that works fine but when I export and run it the program runs but it doesn't have any pictures. Any help would be greatly appreciated
The problem is that once the images/sounds are inside the jar you must access them as a resource.
Image image = new ImageIcon(this.getClass().getResource("/image.gif")).getImage();
Make sure the images are really inside the jar opening the jar with winrar or similar if not add the folders as source folders in your buildpath.

get watij to run

i downloaded watij and am trying to get it to run in eclipse. i tried this sample code
WebSpec spec = new WebSpec().safari();
spec.safari().open("http://www.google.com");
and WebSpec is underlined in red saying WebSpec cannot be resolved into a type
I assume this means that something has not been added into my project.
I right clicked on my src folder and added the .jar files and then added webspec.jar to the project also
Are there any steps that I am missing?
You don't put JAR files in your src folder. They are not source files.
Create a lib folder, put the JAR files there, and then add them to the project's build path.

Categories