I have a Java swing project which i need to test on other computers. I have some classes and a sqlite db which stores login information. I have some lib jars as well. The application takes information from the user and stores it in the sqlite db and then pushes data to the web. I want to package my project so that whoever uses my project has to just double click on an executable or jar.
I tried to Export to a Runnable Jar file in Eclipse but this does not give desired results. After the runnable jar is created and i run it for the first time it creates the sqlite db beside it and does not connect to the web link and send information.
How can i package my data properly?
This is my folder structure -
root
--src
----default package
----rest package
----images
--lib
--file.fcl
--db.sqlite
Any time you run a program outside eclipse that changes any file within a project folder structure, the file changed is "out of sync". To bring things back in sync, right-click on the project in Package Explorer and choose "Refresh" (you can also select the project in Package Explorer and press F5).
If your code is creating the database instead of using the one that is there, it suggests to me that your database location is set for your eclipse environment but not for the general case. For a java application, you have to know (or set) the "current directory" or "current folder" -- this is a folder that is the default for file operations, and from which relative file paths are set. If your current directory is "aaa", and you open a file or database at "bbb/ccc/ddd/database.db", then the "bbb" directory must be within "aaa" or the program won't find the file.
To run your program without having to set the database location, etc., then you're probably going to want to discover the directory in which your program file is stored and set that as your current directory. This SO answer indicates how you might do that.
Related
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.
I am working with one spring boot maven application, so here on registration form customer has to upload two documents.
License
PAN card.
so these documents i am storing on physical location and path i am keeping in DB.
So to store License i have created one folder customerLicense under proejct root folder parellet to POM and for PAN card also i created customerPANcard folder on same location parellel to POM.
and in yml file i have made entry for path, so in code i am reading folder location and storing documents.
application:
customerLicensePath: /customerLicense
customerPANPath: /customerPANcard
So to build the project i run command mvn clean install under the root folder where POM is there. then target folder generated and to run the war i run command java -jar ./target jarName.war that command also i run from root folder.
i don't go inside target because those folder where i am keeping documents that are under project root folder, if i run war file from target folder then will not be able to access those folders.
Here my question is :- Now if i give this war file to client , so first client has to create two folder manually to store documents otherwise he will get error in file upload.
Do we have option like that client don't have to do anything he will only run war file and automatically these folder should get created ?
A few things.
You don't want to put things 'parallel' to the pom.xml. Do this instead:
projectRoot/src/main/java/uniquePackageNameHere/ // all your code in here
/pom.xml
If you want the client to provide the files themselves, simply accept the path at runtime. Something like java -jar jarName.jar -license /path/to/license -pan /path/to/pan (use Apache Commons-cli or something)
If instead you want to provide the files, then include them in the resources folder of your project:
src/main/resources/customerLicense
src/main/resources/customerPANcard
then, in your code, refer to the files by calling:
Main.class.getClassLoader().getResourceAsStream("customerLicense")
Main.class.getClassLoader().getResourceAsStream("customerPANcard")
This will make it such that wherever the code is run from, your program will always go in resources to find them.
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.
If I am building an application in Eclipse using Java, and I want this application to read from a txt file, where should I put the txt file in the package in order to not have to specify its path (so that I can just say "file.txt"). I thought that it was supposed to go in the src folder, but it's not working.
Right click the project folder and click New -> file. The file will be in the Project folder and not in the source folder.
Put the file in the folder from where you run your Java application (your current/working folder). If you're using the default settings of Eclipse to run your application, you should put the file directly inside the Eclipse project folder. To create the file in the Eclipse project, you can simply drag-and-drop it there or right-click on the Eclipse project and create a new file.
The way this can be done is using .getResourceAsStream("file.txt")
SO thread
Downvoted for a correct answer? Wierd...
If you don't want to specify a path, and want to open a File from the file system using the java.io.File API, then put it in the working directory.
In Eclipse, the working directory defaults to the root level of your project, but you can modify it (and also review what it is set to) in an Eclipse Run Configuration. They are accessible under the "Run > Run Configurations..." menu option, and the working directory setting is under the "Arguments" tab for Java programs.
The important thing is for the directory containing your file to be on the classpath. When you're using Eclipse's run dialog for the settings of your project, there's a tab for classpath. You can use it learn what's already on the classpath, and to make additions if you want to.
As mentioned above by #tschaible, the Eclipse working directory defaults to the root of the project. Therefore, the program will look for the file from the root level.
However, if you intend to run the program from the command line, it acts differently because the working directory defaults to the folder that you are running the java file from (aka the src folder). Therefore, it is important to keep this discrepancy in mind when testing your java program from the command line.
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).