I'm new to the programming world, and I am trying to do a simple program where I read and output a single number from a file. I believe I set everything up right in the code, do I need to do anything special with the location of the .txt file to make the program read the file?
package pack;
import java.util.*;
import java.io.*;
public class Ch2_PrExercise17 {
public static void main(String[] args)
throws FileNotFoundException{
Scanner inFile = new Scanner(new FileReader("inData.txt"));
int num1;
num1 = inFile.nextInt();
System.out.println(num1);
}
}
The exception I get is as follows;
Exception in thread "main" java.io.FileNotFoundException: inData.txt (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileReader.<init>(Unknown Source)
at pack.Ch2_PrExercise17.main(Ch2_PrExercise17.java:8)
If Eclipse is really saying "source not found" then it is talking (at that point) about the source code of something.
If you are managing to run the class from Eclipse, then Eclipse has been able to compile it, so it knows where the source code is. So it must be the source code of something else. My best guess is that it can't find the source code for the standard Java class library, and that probably means that Eclipse is running on a JRE installation not a JDK installation.
(If my theory is correct, then the "source not found" text is embedded in the exception stacktrace ...)
It might also be a build problem but I doubt it, because if the class wasn't building you'd see error markers when you view the source file in eclipse, and Eclipse would complain when you tried to run it. Eclipse would also complain if the source file was in the wrong directory.
The second part of the problem is getting your application to find the "inData.txt" file. That should be simple. When you try to open a file with a relative pathname, the Java libraries will try to resolve the pathname relative to the application's "current directory". By default, when you run the application from Eclipse, this will be the directory your shell was in when you launched Eclipse; e.g.
$ cd /home/josh/somedir
$ eclipse &
... and the default current directory should be "/home/josh/somedir".
If the default current directory is not right, you have a number of options, including:
Change directory to the right one before launching Eclipse.
Use Eclipse's "Run Configurations" panel to select your application's launch configuration, and change the launcher's current directory setting.
Change the relative pathname in the source code to the correct absolute pathname for the file.
FOLLOW-UP
Exception in thread "main" java.io.FileNotFoundException: inData.txt (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileReader.<init>(Unknown Source)
at pack.Ch2_PrExercise17.main(Ch2_PrExercise17.java:8)
The "Unknown Source" messages mean that the JVM (not Eclipse) cannot find the information it needs to produce a full stacktrace. You appear to be executing using an "rt.jar" file that has debug information stripped. You can ignore this issue, but to fix it you would need to look into what JVM is being uses, and why the "rt.jar" has no debug info.
The real problem is the FileNotFoundException exception, and my diagnosis of that (as above) stands. The "current directory" is not what it needs to be for that relative pathname to resolve. See above for possible solutions.
Related
So I have a finished java project. It can read CSVs when I press run class on VSCODE. The App class is the main class. However when I compile the project and type "Java App" on the terminal it can't find the location of the CSV. I'm not sure why. This is the error I get
Exception in thread "main" java.io.FileNotFoundException: AIR-QUALITY-PROJECT/lib/1year.csv (No such file or directory)
at java.base/java.io.FileInputStream.open0(Native Method)
at java.base/java.io.FileInputStream.open(FileInputStream.java:211)
at java.base/java.io.FileInputStream.<init>(FileInputStream.java:153)
at DataHandler.readDataFromCSV(DataHandler.java:18)
at App.main(App.java:14)
The CSV is in lib and I'm running App from the src. Again this CSV runs fine when I run the class on VSCODE
I tried putting the 1year.csv on the root, in the src, everything!!! it doesn't work when I compile it
This how I am calling the path
private static final String PATH = "AIR-QUALITY-PROJECT/lib/1year.csv";
Again runs perfectly when I press run class on VSCODE
App.java is in SRC and 1year.csv is in the lib folder
You are using a relative path. That means relative to where the application runs.
If you want to have a reliable application you should use an absolut path, and the best would probably be if you configure or pass the path when running the application.
In such cases it is useful to log what the currentPath is while the program is running. You could do this e.g. like this
System.out.println("Current path is: " + new File("").getAbsolutePath());
All your relative paths must be relative to this path.
P.s. There are various methods to get the currentPath in java. This is just one of them.
I have multiple java files in a single directory that are related to a single program (Main). I have been using the following in the command prompt,
javac -cp . game/textbased/*.java
java -cp . game/textbased/Main
All related files are in a package called game.textbased. The parent folder series is textbasedgame/src/game/textbased.
I am not sure what is going wrong, the java files seem to compile as nessessary, however, when I try to run I get the following:
Exception in thread "main" java.io.FileNotFoundException: items.dat (The system cannot find the file specified)
at java.base/java.io.FileInputStream.open0(Native Method)
at java.base/java.io.FileInputStream.open(FileInputStream.java:216)
at java.base/java.io.FileInputStream.<init>(FileInputStream.java:157)
at java.base/java.io.FileInputStream.<init>(FileInputStream.java:111)
at java.base/java.io.FileReader.<init>(FileReader.java:60)
at game.textbased.WonderLand.loadItems(WonderLand.java:310)
at game.textbased.WonderLand.initGame(WonderLand.java:63)
at game.textbased.WonderLand.<init>(WonderLand.java:24)
at game.textbased.Main.main(Main.java:6)
I am really not sure what is going wrong, and how to run the files from here. I have read other stack overflow inqueries of a similar nature, and generally they say to go from a higher directory but that has not been working for me. I tried going from outside src, or from within the textbased folder, but only going from within the src folder has given me this particular error.
Thanks!
I am using IntelliJ to create a variant of the Java WorldWind application. When I build the JAR artifact in IntelliJ I can see my DLLs and SOs in the top-level directory of the JAR. However, when I try to execute it, it complains it can't see these libraries that are inside the JAR (tried both Windows and Linux, thus being why I include SOs and DLLs).
What do I need to do to get it to see these libraries?
This is my first time creating an executable JAR, so sorry for the vagueness, I'm not quite sure what information is important. Please ask me any questions necessary to better understand this problem, and I will answer them.
Edit:
Catched FileNotFoundException: D:\code\Animator\animator\out\artifacts\Animator_jar\Animator-natives-windows-i586.jar (The system cannot find the file specified),
while addNativeJarLibsImpl(
classFromJavaJar class com.jogamp.common.os.Platform,
classJarURI jar:file:/D:/code/Animator/animator/out/artifacts/Animator_jar/Animator.jar!/com/jogamp/common/os/Platform.class,
nativeJarBaseName Animator-natives-windows-i586.jar):
[ file:/D:/code/Animator/animator/out/artifacts/Animator_jar/Animator.jar ->
file:/D:/code/Animator/animator/out/artifacts/Animator_jar/ ] +
Animator-natives-windows-i586.jar ->
slim: jar:file:/D:/code/Animator/animator/out/artifacts/Animator_jar/Animator-natives-windows-i586.jar!/
Exception in thread "main" java.lang.UnsatisfiedLinkError: Can't load library: D:\code\Animator\animator\out\artifacts\Animator_jar\gluegen-rt.dll
at java.lang.ClassLoader.loadLibrary(Unknown Source)
at java.lang.Runtime.load0(Unknown Source)
at java.lang.System.load(Unknown Source)
at com.jogamp.common.jvm.JNILibLoaderBase.loadLibraryInternal(JNILibLoaderBase.java:551)
at com.jogamp.common.jvm.JNILibLoaderBase.access$000(JNILibLoaderBase.java:64)
at com.jogamp.common.jvm.JNILibLoaderBase$DefaultAction.loadLibrary(JNILibLoaderBase.java:96)
at com.jogamp.common.jvm.JNILibLoaderBase.loadLibrary(JNILibLoaderBase.java:414)
at com.jogamp.common.os.DynamicLibraryBundle$GlueJNILibLoader.loadLibrary(DynamicLibraryBundle.java:388)
at com.jogamp.common.os.Platform$1.run(Platform.java:209)
at java.security.AccessController.doPrivileged(Native Method)
at com.jogamp.common.os.Platform.<clinit>(Platform.java:179)
at javax.media.opengl.GLProfile.<clinit>(GLProfile.java:82)
at au.gov.ga.worldwind.animator.application.AnimatorConfiguration.<clinit>(AnimatorConfiguration.java:46)
at au.gov.ga.worldwind.animator.application.Animator.<clinit>(Animator.java:205)
As far as the structure of the JAR, I don't know how to show other than a screenshot of the JAR in 7-Zip:
In the image we can see that the name of the jar is: Animator.jar, but intelliJ is searching for: Animator-natives-windows-i586.jar. You must go to Module Settings - Artifacts and change the OutputDirectory to: D:\code\Animator\animator\out\artifacts\Animator_jar
Then check the name of the artifact to match the path and the name of the file.
The image added by me is an example.
I have project called test in Eclipse which I want to export into Runnable JAR file. During the process of exporting the project, when I choose as a location of JAR file the subfolder of test folder in file system, such as C:/.../test/App.jar, it will work. However, when I want to put it for example on my desktop, it will run but in a very limited look (some GUI components missing) and it throws a lot of exceptions saying something like that:
at java.awt.EventDispatchThread.run(Unknown Source) xception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: Could not initialize class com.sun.opengl.impl.windows.WindowsGLDrawableFactory
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at javax.media.opengl.GLDrawableFactory.getFactory(GLDrawableFactory.java}
What can be the problem? I assume it is not good that I have to specify my JAR file concretely into one folder in file system, it would become unuseful for any other potentional user.
Looks like the jar needs other jars to execute. In this case you would have to set the classpath before you execute the jar
This might help you with your problem.
Hai
I have an application which is designed in netbeans6.0.1 where i need to display an image as an logo.My coding is as below
mLogo = new JLabel();
URL lUrlLogo = getclass().getResource("/com/images/image.gif");
Image lLogoimage = Toolkit.getDefaultToolkit().getImage(lUrlLogo);
ImageIcon licon = new ImageIcon(lLogoimage);
mLogo.setIcon(licon);
My code works fine when i execute it in netbeans but when I try to run my jar file which is created by netbeans in dist folder,it gives me an exception
Uncaught error fetching image:
java.lang.NullPointerException
at sun.awt.image.URLImageSource.getConnection(Unknown Source)
at sun.awt.image.URLImageSource.getDecoder(Unknown Source)
at sun.awt.image.InputStreamImageSource.doFetch(Unknown Source)
at sun.awt.image.ImageFetcher.fetchloop(Unknown Source)
at sun.awt.image.ImageFetcher.run(Unknown Source)
Whats the problem with my jar
Can some one help me to run my jar
Thanking u
Your jar file will need to include the image.
See this tutorial or this one for instructions for how to do that. Basically if you include the file in a source folder (with the appropriate path) it should be okay.
If you think you've already got the image there, run
jar tvf file.jar
to show the contents. Make sure everything's where you expect it to be in terms of directories.
EDIT: As per the comments, the problem turned out to be due to case sensitivity.
Finding resources in a jar file is always case-sensitive, whereas loading them from a file system is only case-sensitive if the file system is. So if your file is image.GIF but you're looking for image.gif then it'll work when you're loading from the file system in Windows, but not in the jar file.
And you have checked that the JAR file contains the path "/com/images/image.gif"?
Also, I'm not really sure about the leading slash, I always used relative paths to access resources (but I guess it should work anyways).
As an example from class it.lapo.example.Main I use getClass().getResource("resources/logo.png") and in the JAR files are this way:
it/lapo/example/Main.class
it/lapo/example/resources/logo.png
I know... not much different from your own example, but it does work... so I guess yours is quite close to being correct too.