I have a simple question, but I can't find the answer anywhere. I would like to use $PROJECT_DIR$default path variable in IntelliJ.
I wanted to add an icon to the Stage:
stage.getIcons().add(new Image("$PROJECT_DIR$/src/main/resources/com/example/demo/money.png"));
But this doesn't work, unfortunately :(
I tried also using PROJECT_DIR or MODULE_DIR or $MODULE_DIR$, but nothing works. Maybe it's impossible to use those path variables in such way (?). Please, explain it to me.
Using Absolute Path to the image works, but I would like to share my project and that's why I decided to replace it with a path variable.
You don't have to use directory like this one. Intellij already know where is the project.
You have to check according to where will be located the file when it will be exported.
In you case, you have to use :
stage.getIcons().add(new Image("com/example/demo/money.png"));
Because resources are already consider as resources and so will be in main folder in exported jar.
If it failed, you can try with less package, for example put money.png directly in the resource folder
Related
I imported some images into my project javaproject under the folder name images. Now, since earlier the paths were referring to C: drive, I figured I've to change the path. I tried using
this.getClass().getClassLoader().getResourceAsStream("images\\img1.jpg");
I'm not sure what happened there. There was no error yielded whatsoever, but the image is not displaying. I'm fairly new to programming in general so I can't think alternate methods.
How do I re-direct it to the main directory of my project and into my images folder?
Also, I cant have a strict location like:
c:/users/me/java/etc because if this JAR is ever on different computer it won't work.
Any help is appreciated. Thanks!
if you want to use the above approach then all resources files have to be stored under src/main/resources
This will no doubt be marked as a duplicate but I figured I'd try anyway. I've looked at all the related questions that have been asked, and I've searched online, and I can't seem to find the solution to this issue.
I am trying to use the Java FX diffuseMap to texture a Box. However, when using
PhongMaterial material = new PhongMaterial();
material.setDiffuseMap(new Image(getClass().getResourceAsStream("/Eternity/Images/ice.png")));
b.setMaterial(material);
I get a NullPointerException;
java.lang.NullPointerException: Input stream must not be null
I have tried messing with the filename, paths, different ways of accomplishing the same task (all of which still involved InputStreams) and even copying and pasting the actual file path (using Intellij's Copy File Path button) but I can't seem to solve this issue. It just doesn't seem to have any effect on the error.
Here's a look at the project hierarchy, barring the main project folder.
Any and all help would be greatly appreciated!
Class.getResourceAsStream() uses the class loader, to load a resource from the classpath. So, for your code to work, the directory containing the Eternity directory should be in the classpath. Since it's not, the resource can't be found by the class loader, and null is returned, as documented.
So, either change the classpath of your running program, or move the images to the source folder, so that your IDE (and, hopefully, your build/packaging tool), copies the images to the directory where the class files are compiled. And then use the right path of course (i.e. the full package name, but with slashes instead of dots, and with a starting slash). If the image is in src/foo/bar/ice.png, the package is foo.bar, and the path to use is thus /foo/bar/ice.png.
Seems like you're using Intellij, just drag the images directory and drop it in the src directory and rename the path to
... .getResourceAsStream("/Images/ice.png")));
#JB Nizet did a good job of explaining why it doesn't work.
My solution is a temp-fix the right way would be to correctly configure a resource directory and then put your resources there. I suggest you go through Intellij Modules
Firstly i'm 99% certain my path isn't wrong since it's the copied qualified path of the font file. I have no clue why java can't find the file, please help?
Picture of the error code and my code to load up the font
http://i.imgur.com/uEcAIxd.png
The problem is likely that the 1game project isn't on the classpath for your Eclipse run configuration.
Since you're using Eclipse: Click on the little triangle next to the debug symbol or run circle. Then click on Debug Configurations/Run Configurations. Select the run configuration you've been using. Go to Classpath, select User Entries, and then click Advanced. Add folders, then select the root folder for your project (1game). Try running the run configuration again.
It should be noted that this is only a solution to get your code to run provided that the folder where the file in question exists will also exist at the same path after you've deployed your code. Since you've hard-coded the path in, I'm assuming this is the case. However, you might want to confirm. If the resources will be packaged in the jar with the rest of your code, you'll want to use Assets.class.getResourceAsStream instead of a File (Thanks to MadProgrammer for pointing this out).
Is there a way to create user library in eclipse using class path variable to add relative path jars to the library
It's easy. Don't use User Libraries. Instead, use Variables. Example from my workspace:
Open image in a new tab to see full size
Further reference: Here and there
The approach suggested by GGrec is functional and I used it. But "User Libraries" allow some sort of grouping/structuring, for example "Jersey", or "Hibernate", etc.
I found an alternative way to get it working: Created the simplest Eclipse Project. Then inside it created "Linked Resource" Folder using an Eclipse Path Variable. Now I can create the necessary "User Libraries" and "Add JARs" to them, using this Linked resource folder.
In case the location of the jars is changed, the only thing that should be altered is the Eclipse Path Variable, pointing to them.
May be this approach can help someone else.
we have downloaded jar files for lambdaj and its dependencies which are again jar files.
we do not know how to go about it. we have copied these files in the
C:\Program Files\Java\jre6\lib\ext
have set the class path in environment variales as:
variable: classpath
path: C:\Program Files\Java\jre6\lib\ext
but we do not know how to go further. we want to run some lambdaj programs.
can anyone suggest how to run lambdaj programs?
You would run a Java program that requires lambdaj in exactly the same way you'd run any other java program with an external dependency, i.e. by invoking the java executable passing in the fully-qualified name of the Main class, or the JAR with an appropriate manifest, or by deploying it in a servlet container, etc. Additionally you should be putting the LambdaJ JAR on the classpath for this invocation, not in the lib folder for your entire JVM.
What have you tried so far and why/how is it not working? Your question at the moment is a bit analogous to "I want to use Microsoft Word to view some Word documents, how do I do this?".
Update for comment 1: You said "it's not working". That doesn't help anyone address your problem as it gives no clue what you expected to happen and what you observed, only that they were different. As for where JAR files can be stored - you can put them in any directory, so long as that directory is on the classpath (or you add it to the classpath) of the Java application that runs. The canonical place to put external dependencies is in a folder called lib below the root of your project, but the important thing is that you choose somewhere consistent and sensible.
It sounds like you don't quite grok Java and classpaths yet. If you have followed some tutorials and are still stuck, ask for help to let you understand. Adding more detail to your question, including the layout of your files, the commands you issued, and the response that came back would be useful too.
If you are using Netbeans create a project and right click on the Libraries folder within the desired project. Click Add JAR/Folder...