How to put project Input resources outside of JAR like Lib folder - java

I have project which takes Database SQL queries from text files under project folder. When building JAR file I want to put that scripts folder outside of JAR file, but Jar can Access that queries from scripts folder,
i.e., Like when we build JAR file with Netbeans lib folder for that JAR created separately as libraries
same thing I want with script folder but programs can take queries as Input from that folder.
I'm reading queries from text file because my project queries are of about 20 lines and it will change periodically, so I can't add it in actual Java program also I don't want to create DB Stored procedures for the result.
I have shared my projects directory structure
Any help will be appreciated.

Related

what's the ideal way to store files in spring boot application?

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.

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.

adding a .jar file through to a java project through specific code

I need to make such a plugin a storage that contain some .jar files and i need to add these jar files to my java project through code and not through these methods http://www.wikihow.com/Add-JARs-to-Project-Build-Paths-in-Eclipse-%28Java%29 in order to be able to dynamically update my code every time a plugin is addded.
For example , I will put a directory on a server called Plugins that contain .jar files representing different plugins , I need that every time i execute the code I search the directory for new .jar files and if found i can add them to the java project.

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