is it possible to combine unknown format file to jar file - java

i have developed a project in java using swings and mysql. now in order to assign security to the project i want to store the Registration key in an unknown file format like .xyz using serialization. the key entered by the user is compared with the key stored in .xyz file. now, is it possible to add my .xyz file with jar file? if yes how is it possible? please help me to complete the project. thank you.

Jar file is just a zip file. You can add there as many files of any type as you want.
I do not know how your project is organized. If you do not use maven or gradle you probably have to add your file to src folder. If you do use maven add it to resources folder.

Related

Storing data in text files with Java/Maven

I wrote a little Java app for analyzing .csv files. Now I want to keep reading from and writing to a .txt file, which acts similar to a mini-database. For this purpose I simply added the .txt in my project and used the Files.readString(Path) and Files.write(Path path, byte[] bytes) methods.
When I do this in IntelliJ I have no problems but as soon as I build/export the file with Maven and started with the created launcher the app didn't work because the project structure / file organization isn't the same anymore.
I also tried to just add the .txt file to the exported folder afterwards but even then I couldn't manage to implement a relative path to the file.
I'm still relatively new to programming and it's just a small app so I don't think mySQL would fit my needs. I've also read that I could store the data in a property file but I don't know if that would be the right way to archive what I want. Certainly it must be possible to somehow keep a .txt for reading and writing in your exported project. Does someone have an idea?
If you use a ยด*.txt` file for storing, that file cannot be inside the jar because the jar cannot change its contents while running.
You need to put the file somewhere else, either at some dedicated location (let the user choose/configure one), or next to the jar. To figure out your own execution path, you can use
How to get the path of a running JAR file?
Maven is one tricky tool. You need to go to the pom file and add the resource.
Unable to add resources to final jar for maven project in Intellij.
-I hope this helps
Trader

Exported RCP Plugin as jar. file no accsess on CSV file

I wrote a calender plugin in eclipse with rcp and to input some data in my calender I used a csv. file. When I export my plugin as deployable plugin get I a jar. file. Thats fine, but when I add this jar file to another project have I no access on the csv. and cannot change the data in it. If there is a solution for it?
Greets
I am not sure how you have implemented this.But better you keep the .csv file in the plugin itself.Also If you could explain why you are using the csv file, there could be better options than csv file.
Iff you already have added in the project , Make sure you have checked (selected)the file in Build tab of Maifest.mf file(So that it is exported with the jar.)

How to save a .dat file to a certain directory on my desktop

I am writing Java code to generate .dat and .idx files, which I can do.
The files are saved to my workspace folder which I set up as a default when I started Eclipse.
Now I want to save these files and only these files I create to another directory on my server.
Is there code that can do this or do I have to change the settings in Eclipse to do this?
Its a easy thing. Check below if this is what you want. Use
File file = new File("C:\\results\\results.txt");
C:\results\ is your outside directory.
No you can definitely do it programmatically. When specifying the path use something like:
"/c/users/someperson/desktop/blah.dat"

Can you store data inside a .jar?

I'm learning java and am currently trying to develop a simple application. My question is can you store data about settings, etc in a text file internal to a .jar? If so how would you go about accessing this within java? Sorry if this is a really stupid idea.
InputStream is = this.getClass().getResourceAsStream("/data/file.txt");
The resources you are getting need to be on the classpath
Yes you can, and it's not a stupid question we all need to start somewhere.
There are two parts to your question:
Adding a data/text file to a .jar - (using ant to jar it:) add "fileset dir=..." to the jar target, where dir is set equal to the directory that has the data/text file. Refer to How can I include data text files in a jar using Ant?
Accessing that data/text file from within the java code - you need to use a ClassLoader and getResourceAsStream. Refer to Loading files in JAR in Tomcat using getResourceAsStream
Also, please take a look at https://github.com/gitjonathan/turbo-sansa, I have a working version up on it.
Can you store data inside a .jar?
Read-only data can be stored inside a JAR file. You can read such data using getResourceAsStream(...) if the JAR is on the classpath, or by using the standard JAR file API class if it is not on tle classpath.
But storing update-able data in a JAR file is problematic:
In a lot of circumstances it is impossible; e.g. because the JAR file is read-only or was downloaded on the fly.
In all other cases it would be very awkward, because the standard JAR file API class does not support update in place. (You would need to create a new ZIP file, copy across the old content apart from the file you are updating, add that file, and then rename the resulting file.)

How to use a code within a .jar file while writing a program in NetBeans

I am trying to use the jAudioPackage in a class project. I need it's functionality of getting the id3 tag info from the header of a MP3 file. I looked at the website and once you have everything set-up it seems pretty easy to use. I am just having trouble with actually importing the .jar file which contains the code. How would I go about this in NetBeans?
To summarize, I have the .jar file I just dont know how to get to use the code within the file.
Right click on your project and go to Properties -> Libraries -> Add JAR/Folder and select the jar file in the following dialog. It should then have access to it when coding/compiling.
You need to add the jar file in your project's libraries. Read http://netbeans.org/kb/docs/java/project-setup.html#projects-classpath.
Please read the documentation for the library as to what the calls are and then call the required methods in order to accomplish what you are doing. Before you do that you will have to import the jar in to your project...

Categories

Resources