So I've created a game, runs perfectly in eclipse--all of the images load and it reads properly from a text file. The text file provides level info.
This is where my problem comes in.
I exported it all to an executable .jar file, and it found all of the images. But it did not find the .txt file. Where does the .txt file need to be for it to be found?
try{
controlBoard = new Scanner(new FileInputStream("lvl1.txt"));
}catch(Exception e){
gb.error = true;
}
You should not access that file using FileInputStream. Get an InputStream using the class loader and getResourceAsStream(). It'll find it in the JAR.
Related
I am making a game and have text files inside of my resources folder that I use to store unit data. While running my game from my IDE, I have no problems with the code using Googles Guava to load the files to memory. However, when I tried to package it as a jar, loading text files using the Files methods causes crashes.
I have seen that input resource stream may be a solution to properly load text file data from txt files stored inside of the jar. Could someone please show me how to use the method properly to load from input resource stream directly to a String.
Thank you
String forestString = this.forestkinFileMap.get(forestKin);
URL url = Resources.getResource(forestString);
String text = null;
try {
text = Resources.toString(url, StandardCharsets.UTF_8);
} catch (IOException e) {
e.printStackTrace();
}
UnitTile unit = this.unitTileFactory.processUnitCodeComposite(forestString);
return unit;
Also, this is for the client program, but since its just a matter of loading text files inside the jar, this shouldn't be a relevant factor
The name of a resource must be an archive entry’s name.
A .jar file is actually a zip file, usually with some Java-specific zip entries in the archive. This means you can open a .jar file in any zip tool.
In Windows, you can make a copy of your .jar file, rename it so it has a .zip extension, and double-click it.
Of course, every JDK has a jar tool, so you can just use that tool to view the .jar file’s entries:
jar tf C:\path\to\program.jar | more
If you see this:
com/nbulgarides/game/data.txt
Then you have to use that path when loading a resource:
Resources.getResource("com/nbulgarides/game/data.txt")
The string argument is not a file name and you cannot just pass any file name to a getResource method. A resource path is always relative to the root of a .jar file (or, in theory, a directory that’s on the classpath, though no application is distributed to users that way). Also, resource paths always use forward slashes (/), on every platform, including Windows.
You don’t need a guava class to do this. Regular Java SE classes can do it:
String text;
try (InputStream textSource =
MyGame.class.getResourceAsStream("/com/nbulgarides/game/data.txt")) {
text = new String(textSource.readAllBytes(), StandardCharsets.UTF_8);
}
FileInputStream fstream = new FileInputStream("abc.txt")
is throwing a FileNotFoundExceptionn while running as a jar. Why ? Normally it is able to find while running from main method.
class MyClass{
InputStream fstream = this.getClass().getResourceAsStream("abc.txt");
}
This code should be used.
And the files(in this case abc.txt) should be kept , in the Object references class location. That means , this.getClass refers to the location of some folder i.e, com/myfolder/MyClass.java folder .
So we should keep the abc.txt in com/myfolder this location.
If your file is packaged with your jar then you should to get information using getClass().getResource(url):
FileInputStream inputStream =
new FileInputStream(new File(getClass().getResource(/path/to/your/file/abc.txt).toURI()));
Else you need to create it always in the same path with your jar and you can get it like you do :
src/myJar.jar
src/folder/abc.txt
FileInputStream fstream = new FileInputStream("folder/abc.txt");
You can read here also :
How do I load a file from resource folder? and File loading by getClass().getResource()
You can use FileInputStream only when you actually have a file on the computer's filesystem. When you package your text file in the jar file for your program, it is not a file in the filesystem. It is an entry inside the jar file.
The good news is that it is even easier, in Java, to access the file this way: it is in your classpath, so you can use getResourceAsStream().
InputStream stream = getClass().getResourceAsStream("abc.txt");
If you have your classpath set up correctly, this will work regardless of whether it is a file in a directory (such as during development), or an entry in a jar file (such as when released).
It's because your working directory will probably be different under the two environments. Try adding the line
System.out.println(new File("abc.txt").getAbsolutePath());
to see where it is actually looking for the file.
i am making a program in which i have to allow the user to browse an image and store it with its details in image folder inside the source cod
my code is working working in netbeans but when i make jar file the code is cot able to store image.
i have stored some images through netbeans and able to access them using image/imanename.jpg but can't able to store images.
help me as soon as possible. thank you
the code i tried is
File f = new File(s);
long size=f.length();
FileInputStream fis1=new FileInputStream(f);
FileOutputStream fos2=new FileOutputStream("image/"+tfpn.getText()+".jpg");
byte b[]=new byte[10000];
int r=0;
long count=0;
while(true)
{
r=fis1.read(b,0,10000);
fos2.write(b,0,10000);
count = count+r;
if(count==size)
break;
System.out.println(count);
}
System.out.println("File copy complete");
When you generate a jar file, it is in a compressed format (like .zip). You cannot simply save a file into the jar. It is technically possible, but is complicated. See here for more info.
The reason it works in netbeans is because when you run a file in netbeans, the code is not yet compressed into a jar. Once you make the jar file, you cannot simply write files into the jar. Try and save your file in a normal directory (not within the jar).
I have a Java application in Eclipse that references .XML files as templates for other functionality. Usually I package the .JAR file without these files, because placing them within the same folder as the .JAR file seems to work fine with this reference:
File myFile = new File("templates/templateA.xsd");
I now require that these templates be placed within the same .JAR file as this application. I can include them with no problems, but these references no longer seem to work.
Is there a correct way of referencing the .XML file from within the same .JAR that the application is running from?
You need to know how to load the files from class path.
one of the ways is as follows
class XMLLoader {
public String loadXML(String fileName){
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(fileName);
// do the loading of the file from the given input stream.
}
}
you know that the "templates" folder should be inside of your jar.
If you just need to read this file, you might not need a java.io.File but just an InputStream that you can get via
this.getClass().getResourceAsStream("templates/templateA.xsd")
If you really need a java.io.File... I do not know... The last time a really needed a File, I just copied the InputStream to a temporary file but this is ugly.
I'm programming Java in Eclipse IDE. Here is code I want to read file:
File file = new File("file.txt");
reader = new BufferedReader(new FileReader(file));
I put file.txt in two place:
1) same folder of this SOURCE file.
2) in bin\...\ (same folder of this CLASS file)
But I allways receive NO FILE FOUND.
Please help me.
thanks :)
If the file ships with your application, it would be better accessed as a resource than as a file. Simply copy it to somewhere in your build path and use Class.getResourceAsStream or ClassLoader.getResourceAsStream. That way you'll also be able to access it if you bundle your app as a jar file.
Currently, you're looking for the file relative to the process's current working directory, which could be entirely unrelated to where the class files are.
if you put the file under sources and inside the package "test" for example, the path is:
./src/test/file.txt
you can use
File file = new File("./src/test/file.txt");
System.out.println(file.exists());
The path ./bin/test/file.txt will work in the second case and is more suitable for a normal java project