Reputation: 91
Have made this program that should open an image on my desktop but it gives me the error java.lang.ArrayIndexOutOfBoundsException
when I run it. What I done wrong?
import java.awt.BorderLayout;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
public class Image {
public static void main(String[] args) throws Exception
{
new Image(args[0]);
}
public Image(final String filename) throws Exception
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
JFrame editorFrame = new JFrame("C:/Users/Username/Desktop/index.jpg");
editorFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
BufferedImage image = null;
try
{
image = ImageIO.read(new File(filename));
}
catch (Exception e)
{
e.printStackTrace();
System.exit(1);
}
ImageIcon imageIcon = new ImageIcon(image);
JLabel jLabel = new JLabel();
jLabel.setIcon(imageIcon);
editorFrame.getContentPane().add(jLabel, BorderLayout.CENTER);
editorFrame.pack();
editorFrame.setLocationRelativeTo(null);
editorFrame.setVisible(true);
}
});
}
What do I get this error:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at Image.main(Image.java:15)
Upvotes: 0
Views: 478
Reputation: 767
You have to pass command line argument to main()
method. That argument we are passing through Image()
constructor.
new Image(args[0]);
Please take a look on Command Line concept below.
What is Command Line Argument?
A Java application can accept any number of arguments from the command line. Those arguments called as Command Line Arguments. This allows the user to specify configuration information when the application is launched.
How to pass command line arguments?
The user enters command-line arguments when invoking the application and specifies them after the name of the class to be run.
For example, suppose a Java application called Sort sorts lines in a file. To sort the data in a file named friends.txt, a user would enter:
java Sort friends.txt
Here: Sort-- classname
friends.txt --- command line argument.
When an application is launched, the runtime system passes the command-line arguments to the application's main method via an array of Strings. In the previous example, the command-line arguments passed to the Sort application in an array that contains a single String: "friends.txt".
Update:
You can pass multiple command line argument with Space as a delimiter.
like
java Sort stack.txt overflow.txt
How to pass Command line Arguments in Eclipse ?
Go to program on package explorer or Navigator view
Select that program ---> after that right click on program select Run As ---> Run Configuration ---> select Arguments second option on top ---> give arguments on Program Arguments
See the below Screen Shot:
Upvotes: 1
Reputation: 866
Look like you forget to pass the comand line args.
new Image(args[0]);
Upvotes: 0