Placing application files in bin folder - java

How can I place the files being used by my application, more particularly text files that contains data that my Java program uses. For the images since it is static, I just copy and paste them in the bin folder. But I have some text files that I create during runtime and I don't know where to place them. I need a place where I can save them in and edit them sometime.
By the way, I am using eclipse IDE.
And how would I code it? Like retrieving etc.
I am reading files with Scanner, creates them with Formatter

If you using Eclipse IDE you can just place the text files in the source folder and eclipse will copy them to the bin folder when building the application. When editing the files in the source folder within Eclipse it will update the copy in the bin folder. When you edited them with an external program you need too choose the “Refresh” menu item in the Eclipse IDE.
Place them relative to the .java file of the class using them so that the copy in the bin folder will be relative to the .class file as well. The you can access them via MyClass.class.getResourceAsStream("path relative to MyClass.class"); which gives you an input stream. This works even if you package your application as a JAR file.

Related

Is the relative path for a java project different for Intellij and cmd?

I have a java assignment, and at a specific point, we have to create a new folder and write some text files there. The issue is, when testing my code on Intellij, it works fine, but when testing it on cmd, I need to change it a tad bit?
My project structure:
.../project/src/greedycomparisons // greedycomparisons is the file where I'm creating the dir from
.../projct/data // the directory I want to create, to have my text files in
More specifically:
I'm trying to create the new subdirectory from a file I have inside my src folder. Therefore, the thing I tried first was:
File directory = new File("./data/");
if (!directory.mkdir()) dosth; // nothing happens on first call
/* making the String I want as my file's name, say string */
File file = new File(directory, string);
Which works fine (on Intellij), creating the subdirectory (and the files later on) exactly where I want them to. And then, likewise, I try accessing the files I made there from another file in my src folder, by again using "./data" before the name of the file I was trying to access, which again works as expected on Intellij.
But when I tested this on cmd, I need to change the directory name to "../data" in order for the code to work, which in turn does not work on Intellij (specifically giving me a "The system could not find the path specified" error) when I change it to that. Given that I have to submit it as an assignment and I don't want any ambiguity in regards to my files, is there something more "universal" that I can try, so my code works regardless?
If you check your IntelliJ configuration, by clicking at the top where you have the name of the file you are executing, you can click on Edit Configurations, and you will see one of the fields you can configure, called "Working directory".
IntelliJ is smart enough to set it to your project folder, outside the /src folder, because it is not good practice to write data inside your /src folder which should only have source files, unless they are resources you want to commit with your code.
On the other hand, when executing it from cmd, your working directory is just where you are trying to run the compiled program from. You haven't specified in which folder you are in when doing so, but most probably you are inside some subfolder that has the classes, which is why you needed to do the ...
If you package your classes in a .jar file, the way IntelliJ made it should work. If you run your .jar file with java -jar myjarfile.jar and the ./data folder is at the same level, then it would work.

Use File Input and Output in .jar FIle

So I've created just a simple application which I'm using to apply for a highschool internship. It was built using Eclipse.
I initially exported into a runnable .jar file, but the location I initially saved files, ("src/fileDirectories") didn't work on export.I then set the location to "./fileDirectories") which works and the app can actually run, however the .jar file creates the folder directory in the same folder as the .jar file.
This isn't too bad, as I can create a folder with the .jar and the saved files in it, which is fine, but similar to images, I'm wondering if there is a way to save .txt files in a directory to the .jar file so everything works with just the .jar application.
Assuming the files you want to access are static, and are not changed by the application, you can do this using the classpath.
When you access a file using new File("path"), Java will look for it in the file system, relative to the working directory (i.e. where the application was launched from.
An alternative way to access files is to look them up from the classpath - this is the list of locations Java searches for resources. It includes, among other things, the top level of your JAR archive. You can access this as follows:
this.getClass().getResourceAsStream("/my_file.txt")
Build tools will generally have preconfigured directories (e.g. src/main/resources) which get copied from your source tree into the JAR so they can be accessed in this way.
This is great if you have some static resources or config which you want to access at runtime, but don't want to have to pass around with your application.
However, if what you want is a working folder for files which your application will make changes to or create new instances of, like user documents, you don't want this to be editing the JAR - you'll need to make a separate folder for these.

How do I deploy applets(class files) in netbeans?

my project has 2 JSwing applets(no main class).
working: thing is they have to be together as they both work upon the same database. one stores, the other reads and process.
problem: in netbeans i used build and clean& build option but they seem to generate only .jar file and no .class file in "dist" folder.
situation:i want to embed these 2 applets separately on different html pages.
how do i achieve this?
Problem
In Netbeans i used build and clean& build option but they seem to
generate only .jar file and no .class file in "dist" folder.
JAR file itself is called Java Archive. It is nothing but a bundle of class files.
You can get your .class files by using any archive utility like winzip, 7zip etc...
Just right click on your .jar file > open with winzip/7zip > and drag and drop the contents inside it to your preferred directory.

Location of image in exported project folder

In my current code I write the specific location of the images I want to use in my project, now, these locations are only correct until I move the image to a different directory or open the application in a different computer.
Where do I place the images (where to place them in the exported folder) I want to use in my project?
The exported project is a zip file, once extracted, I have 2 folders within the extracted folder, one named nbproject other is src, one text file named build another .mf file called manifest inside nbproject I have two text files and two .properties files inside src I have my four classes.
Where do I put the images I want to use in the project? And once I place them, what directory do I write in my project?
Here's an example of how I use an image:
// Main menu background image
bgg[0] = new ImageIcon("D:/NetBeans/NetBeans projects/Java/Project Images/bg option for Vanguard.jpg").getImage();
And then I draw the image all over the screen
My exported folder contents:
http://i.imgur.com/qd4PeJo.png
http://i.imgur.com/W5YQ7OC.png
EDIT: Thought it worked but it keeps giving an exception on other people's computers, perhaps this isn't the reason beause I moved the images around in my PC and it worked but still.
Since you are writing the directory in your code.
bgg[0] = new ImageIcon("D:/NetBeans/NetBeans projects/Java/Project Images/bg option for Vanguard.jpg").getImage();
When you run the project in another computer or move the image to another location, the program can't find the path to the picture, so there is an error. Instead of using the full path. Copy the images to your project folder and use relative paths.For instance : "images/Vanguard.jpg".
The path where your program looks for resources can depend on how you run the program. If you ran it via console/terminal the initial path starts at the path of the *.class file you ran. If you run it from an IDE this path may change, in Eclipse the path starts at the Project dir and not the src folder. You can find out the exact path by calling System.getProperty("user.dir") that will return the current working path as a String.
Don't use a strict path, use a relative path like so:
Main.java
String path = "Project Images/bg option for Vanguard.jpg";
bgg[0] = new ImageIcon(getClass().getResource(path)).getImage();
In order for this to be loaded, you must place a folder called Project Images in the directory in which your Java files are. Then place the image bg option for Vanguard.jpg in this folder. Be sure to compile the program in you IDE so it can make a copy for the compiled version.

How to build a java project with all the required files and folders?

I created a simple application which will read all the files and folders kepts inside a folder. Whenever I build the project, I only get the jar file but the folder where the file and folders are kept have to be created. Is this supposed to be like this or I have to code something, which will create the folder upon the final build.
Compilation gives you only executable file (i.e. *.jar). If you need something else to make you application works - you must do it (programmatically or not).

Categories

Resources