I’m trying to solve this problem for about 2 days. I’ve to create runnable JAR. I’m using Eclipse (newest version), Java SE 10 on macOS Sierra.
So, this is the only class in the test project.
package test_package;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
public class Tester {
public static void main(String[] args) {
try {
BufferedImage testImage = ImageIO.read(Tester.class.getClassLoader().getResourceAsStream("/test.jpg"));
} catch (IOException e) {
e.printStackTrace();
}
}
}
This is test project file structure (without test.jpg file):
TestProject
- bin
-- test_package
--- Tester.class
- src
-- test_package
--- Tester.java
I tried four different options:
BufferedImage testImage = ImageIO.read(Tester.class.getResourceAsStream("test.jpg"));
BufferedImage testImage = ImageIO.read(Tester.class.getResourceAsStream("/test.jpg"));
BufferedImage testImage = ImageIO.read(Tester.class.getClassLoader().getResourceAsStream("/test.jpg"));
BufferedImage testImage = ImageIO.read(Tester.class.getClassLoader().getResourceAsStream("test.jpg"));
If test.jpg was placed in the root of the project, in src or test_package directory, the compiler always throwing the same error:
Exception in thread "main" java.lang.IllegalArgumentException: input == null!
If I copy test.jpg to bin directory, this two options give the correct result:
BufferedImage testImage = ImageIO.read(Tester.class.getResourceAsStream("/test.jpg"));
BufferedImage testImage = ImageIO.read(Tester.class.getClassLoader().getResourceAsStream("test.jpg"));
But when I am trying to create Runnable JAR, the same error appears. When trying to create JAR, I copied test.jpg to each directory of the project.
I also tried to change option in Eclipse Library handling when creating Runnable JAR (I tried all three options for the project with all four command options). In each case the error appears.
About the Source directory. In my case, the source directory is src.
So, I don’t know what to do next. I tried almost everything with no result. Any ideas?
Optional<InputStream> resourceAsStream = Optional
.of(Thread.currentThread().getContextClassLoader().getResourceAsStream(
"/test.jpg"));
Try this to get a stream then it can be converted to image
Related
ok i am a new one here and tried to write an awesome program:
package f;
import javax.swing.*;
public class dasMain {
public static void main(String[] args) {
ImageIcon img = new ImageIcon("pics/daFaq.png");
JOptionPane.showMessageDialog(null, img, "u r heck", JOptionPane.PLAIN_MESSAGE);
}
}
the thing is that if I run the program from Intellij Idea, then everything works fine, but after compilation the picture disappears
here are the source files of the project:
https://i.ibb.co/Njc8jYp/screen.png
i want to run this awesome code with pictures on other computers, but i only know this way and it doesn't work :(
You probably do not know where your program expects the picture to be. If you modify your code slightly, this information would be evident. Make use of
ImageIcon(URL)
File.toURI()
URI.toURL()
With that your code can look like this:
package f;
import javax.swing.*;
import java.io.File;
public class dasMain {
public static void main(String[] args) {
File png = new File("pics/daFaq.png");
System.out.println("Loading image from " + png.getAbsolutePath());
ImageIcon img = new ImageIcon(png.toURI().toURL());
JOptionPane.showMessageDialog(null, img, "u r heck", JOptionPane.PLAIN_MESSAGE);
}
}
Also I am pretty sure you intend to ship the png together with your code, so you better load it as a resource from the classpath. Here is an example.
I also investigated a bit why you would not see any error message or exception. This is documented in ImageIcon. So you might want to verify the image was loaded using getImageLoadStatus().
If you access the resource with the path like pics/file_name.png, then the pics - is the package name. And it must be located in the directory, marked as resource type. For example, create the directory, named resources in your project root, mark this directory as resource type, and move the pics there:
P. S. I would advise to use Maven or Gradle build system for managing project builds. As it is commonly accepted build management systems for the JVM projects. IDE New Project Wizard has the option to create Maven or Gradle based projects.
I have a Java program that runs fine in Eclipse - when I export an executable jar file it gives me an exception.
I know it is because I am giving it the wrong path but I do not know how to fix. I tried using getClass().getResourceaAsStream() (or sth. like that) but that did not work as well.
The code I am using at the moment is the following:
line 31:
BufferedImage image = ImageIO.read(new File("/src/res/loading.png"));
Okay, so I created a simple project, made a directory called res and threw some images into it
Before anyone points out that this not Eclipse, it shouldn't matter, we've already established that the Jar file contains the res directory and images, it's about replicating the structure
I then wrote a really simple example...
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
System.out.println(Test.class.getResource("/res/0.jpg"));
System.out.println(getClass().getResource("/res/0.jpg"));
}
}
which outputs...
file:/.../Test/build/classes/res/0.jpg
file:/.../Test/build/classes/res/0.jpg
No, before any points out the IDE's environment is different from the command lines, I also ran it from the command line...
java -jar Test.jar
jar:file:/.../Test/dist/Test.jar!/res/0.jpg
jar:file:/.../Test/dist/Test.jar!/res/0.jpg
public void loadStdImage() throws IOException
{
Image image = ImageIO.read(this.getClass().getResource("/Resources/Images/Student/Capture.png")); //Line 350
ImageIcon icon = new ImageIcon(image);
JLabel lblImage = new JLabel(icon);
lblImage.setIcon(icon);
lblImage.setBounds(753, 50, 149, 171);
add(lblImage);
}
I tried many things... but nothing works out. Continuously showing the following run-time error
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: input == null!
at javax.imageio.ImageIO.read(Unknown Source)
at View.Student.loadStdImage(Student.java:350)
Project folder structure is:
edit:
Found the solution. See the change of icon of the resource folder in the following picture and the above image. I added my resource folder to Java Build Path. Right click on your project, go to properties, then select 'Java Build Path', from there add your folder to java build path.
Cheers
enter image description here
welcome to SO. As you are new here, please read this - https://stackoverflow.com/help/mcve
Let me help you with this for now.
I have standard Eclipse project:
and my test class looks like (minimal):
package q34460547;
import java.awt.Image;
import java.io.IOException;
import javax.imageio.ImageIO;
public class LoadTest {
public static void main(String[] args) throws IOException {
new LoadTest().loadStdImage();
}
public void loadStdImage() throws IOException {
Image image = ImageIO.read(this.getClass().getResource("/ScreenShot005.png"));
}
}
and now, when I used
ImageIO.read(this.getClass().getResource("/ScreenShot005.png"));
image is loaded from res so called source folder in Eclipse.
When I used
ImageIO.read(this.getClass().getResource("ScreenShot005.png"));
image s loaded from the folder in which LoadTest.java file is (to be precise it is also compiled to same folder - in Eclipse it's bin).
You can find more info for example here - What is the difference between Class.getResource() and ClassLoader.getResource()?
edit:
The image has to be on classpath (when using Class.getResource), that's why it was not loaded from Resources folder. There are two options, use another version of ImageIO.read() or make your Resources folder a source folder:
It's the first day of learning JavaCV for me. And this is the first test example which I use just to make sure that my setup is done correctly. Unfortunately I can't run the example and I barely understand the code and all dependencies so it's really hard for me to find out what is missing. Below I'll post my project setup and also the errors I get.
Also the example I use is from: http://ganeshtiwaridotcomdotnp.blogspot.com/2011/12/javacv-capture-save-flip-show-live.html
code
import static com.googlecode.javacv.cpp.opencv_core.cvFlip;
import static com.googlecode.javacv.cpp.opencv_highgui.cvSaveImage;
import com.googlecode.javacv.CanvasFrame;
import com.googlecode.javacv.FrameGrabber;
import com.googlecode.javacv.VideoInputFrameGrabber;
import com.googlecode.javacv.cpp.opencv_core.IplImage;
public class GrabberShow implements Runnable
{
//final int INTERVAL=1000;///you may use interval IplImage image;
CanvasFrame canvas = new CanvasFrame("Web Cam");
public GrabberShow()
{
canvas.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
}
#Override
public void run()
{
FrameGrabber grabber = new VideoInputFrameGrabber(0); // 1 for next camera
int i = 0;
try
{
grabber.start();
IplImage img;
while (true)
{
img = grabber.grab();
if (img != null)
{
cvFlip(img, img, 1);// l-r = 90_degrees_steps_anti_clockwise
cvSaveImage((i++) + "-aa.jpg", img); // show image on window
canvas.showImage(img);
}
//Thread.sleep(INTERVAL);
}
}
catch (Exception e)
{
}
}
}
I use Eclipse and it seems like all imports are there.
Here is a print screen of my project as it looks like in Eclipse:
So this is it for the setup. I try to get something out of this. When I right click on the project and try to Run it as Java Application the following windows is showing up:
You can see my choice at the top of the window. The I get an error window A Java Exception has occurred and here is part of the error I get in the console window:
Exception in thread "main" java.lang.UnsatisfiedLinkError: C:\Users\Xman\AppData\Local\Temp\javacpp4929678155627\jniopencv_core.dll: Can't find dependent libraries
at java.lang.ClassLoader$NativeLibrary.load(Native Method)
at java.lang.ClassLoader.loadLibrary0(Unknown Source)
at java.lang.ClassLoader.loadLibrary(Unknown Source)
at java.lang.Runtime.load0(Unknown Source)
at java.lang.System.load(Unknown Source)
at com.googlecode.javacpp.Loader.loadLibrary(Loader.java:422)
at com.googlecode.javacpp.Loader.load(Loader.java:372)`
I've read the error, it seems like the problem is caused by missing libraries but I'm not sure, also I have followed the instructions (which weren't that many in fact) and from the comments below the example code it seems that it actually works. So I guess the problem is somewhere here - in my project, code, setup, but I can't find what I'm missing or doing wrong.
This error occurs when your opencv dll are not set in System path.
If you have extracted your opencv folder in C:\ directory then set your path as following
For 32 bit:
C:\opencv\build\x86\vc10\bin;C:\opencv\build\common\tbb\ia32\vc10\
For 64 bit:
C:\opencv\build\x64\vc10\bin;C:\opencv\build\common\tbb\intel64\vc10\
If you have extracted in different location then change the path accordingly.
In order to set path you can go to Control Panel > System Security > System > Advanced System Settings > Environment Variables. In System variable select path and click on Edit and insert above locations and restart windows.
you can find detailed instructions at http://opencvlover.blogspot.in/2012/04/javacv-setup-with-eclipse-on-windows-7.html
Download and install : Microsoft Visual C++ redistributable package (32-bit): vcredist_x86.exe
I am currently putting a program into a .jar, and have difficulties telling it where to get its data from. The data was inside of a file in the project, and I am sure that it is located in the jar as well. But I have no clue on how to get a path into a jar.
I found the getClass().getClassLoader().getResourceAsStream() method online to get an input stream into the jar, but since I used FileReaders all the time, I dont know what to do with it as well..
I`d be very thankful for any help.
Edit:
Here is a picture of how the directory is organized:
My command window shows what happens if I run the .jar. Nullpointer in line 30. I tried it with and without .getClassLoader(), it just wont find it.
Here is the inside of the jar:
again, app is where the class files are in. Hence, via class.getResource.. I should be able to search in DataPackeg. Man, this is wearing me out.
A key concept to understand is that files don't exist inside of jars. You must instead get your data as a read-only resource, and you will need to use a path that is relative to path of your class files.
If you're still stuck, you may need to tell us more specifics about your current program, its structure, what type of data you're trying to get, where it's located in the jar file, and how you're trying to use it.
For instance, say your package structure looked like this:
So the class file is located in the codePackage package (this is Eclipse so the class files live in a universe parallel to the java files), and the resource's location is in the codePackage.images package, but relative to the class file it is the images directory, you could use the resource like so:
package codePackage;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.*;
public class ClassUsesResources {
private JLabel label = new JLabel();
public ClassUsesResources() {
try {
BufferedImage img = ImageIO.read(getClass().getResourceAsStream(
"images/img001s.jpg"));
ImageIcon icon = new ImageIcon(img);
label.setIcon(icon);
JOptionPane.showMessageDialog(null, label);
} catch (IOException e) {
e.printStackTrace();
System.exit(-1);
}
}
public static void main(String[] args) {
new ClassUsesResources();
}
}