Netbeans:Java prevent Folder delete while cleaning project - java

I am creating a JSF application using Netbeans and i had a download folder called Snap and Files which stores the client uploaded Snaps and Files respectively but when i clean the project whole build folder gets deleted and so my files. Is there any way to prevent it from deleting some folder or any alternative.(I dont want to save it on any static path.)

You can and should not store the uploaded files in expanded WAR folder. They would in a real production environment also get lost simply because files which are added during runtime are not contained in the original WAR.
You should store them on a fixed path outside the deploy folder. There are several ways to configure the webapp to use it and to configure the server to serve files from the folder by a virtual path. See also this answer for a detailed explanation and examples: Uploaded image only available after refreshing the page

Related

Backup folder for 'semi-persistent' files beside Target folder within Maven/Java project? Best practice?

As I understand, within a Maven/Java project the target folder is the standard place to store non-persistent files including log files etc. With e.g. mvn clean one can purge it and normally it's not under git.
But working on a JUnit project I would like 'backup' log files from failed runs temporarily. It makes no sense to 'backup' into the target folder, because this 'backups' shouldn't be purged until I finished the failure analysis after some days or weeks.
Otherwise, because it's not a real 'backup', I wouldn't copy those files into a real backup folder and littering backup media.
So I created a separate folder 'errorlogs' beside the target folder, of course, it must be added to .gitignore. I'm wondering if there is a best practice to store such 'semi-persistent' stuff in a kind of standard folder within a Maven/Java project?

Eclipse showing incorrect project path

I have a maven web project in eclipse. I need to get the project's path, actually have to get list of files under src\main\resources\someFolder in project.
Tried String dataDir = "src\\main\\resources\\someFolder";, on running this directory structure is created inside eclipse folder like F:\softwares\eclipse\eclipse\src\main\resources\someFolder. Same when using / instead of \\.
Tried System.getProperty("user.dir")and new File(".").getAbsolutePath(), they return F:\softwares\eclipse\eclipse.
I need to access the project folder in my workspace F:\workspace\Project\src\main\resources\someFolder
But when created a core java app and used System.getProperty("user.dir")and new File(".").getAbsolutePath(), I am getting project path in workspace, F:\workspace\Project. This src\\main\\resources\\someFolder also works fine then.
Why this odd behavior from eclipse?
As mentioned here the directory user.dir is the place where the JVM is started. As web applications are mostly jar/war/ear packages placed somewhere within the folder of the server eclipse handles them in a different way because the behaviour of such a web application is different. You cannot expect to have file access from outside the jar/war/ear file. Within the jar/war/ear file everything from within src/main/resources will be available just by using getResourceAsStream as described in many other stackoverflow articles. This way you mustn't use src/main/resources/myfile.txt but myfile.txt.
Don't try to guess or use what the user.dir / JVM/server start folder is!

Gradle - JARs copied into WEB-INF getting overwritten by GoogleAppEngine?

I'm currently using Gradle to compile and upload my Java code to GoogleAppEngine. It compiles fine locally, the problem is that the JAR libraries that the code is dependent upon are not within the "WEB-INF" folder on GAE.
I'm currently using War to copy the JAR files to WEB-INF using:
copy {
from 'libs'
into 'build/exploded-war/WEB-INF'
include '**/*.jar'
}
The problem is that if it does this before the "gaeUpdate" command then the WEB-INF folder is overwritten and the JARs are deleted and thus not uploaded. If it does it after the gaeUpdate command then it's too late, the files have already been uploaded to the server and they are thus not uploaded.
It's important to note that if I enter "gradle" in the console without the "gaeUpdate" then the JARs don't get deleted, it's only when the "gaeUpdate" is added that they appear to get removed.
Is there way to upload the files after the "build" folder has been created but before the server upload?

where does tomcat store my files?

I've created a web application using Netbeans. Before, when I was running the web app via netbeans and tomcat server (which was a zip), all my external files (uploaded files and other helper files I use for my app) are stored in bin directory.
Now, I tried installing an apache tomcat service using windows installer because I wanted to know how to deploy the project on a dedicated server. I have successfully deployed the war file using tomcat's deploy utility. However, when I run the project via the apache tomcat windows service, it is not saving the files in bin dir and it cannot read my files that I pasted in bin dir, too.
Where do you think should I place my files?
EDIT: Upon observing the tomcat service directory, I found out that it is store in the root. If I have my tomcat installed at 'E:\Apache\services\tomcat\', it is stored at the 'tomcat' directory.
Ultimately, it is what your application does that determines where the files are stored.
By the sounds of it, your application is storing files in the current directory of the JVM, which happens to be the "bin" directory when you launch the web server via NetBeans. If so, you will find them, in whatever the current directory is when Tomcat is launched as a windows service.
Frankly, I think you've got this wrong. You should be making a conscious decision as to were uploaded files should be stored, and then making sure that the upload mechanism you are using puts them there.
Obviously, putting them in the current directory is a bad idea. You don't want them being stored in different places depending on how the web container is started. And obviously the "bin" directory is an inappropriate place. (What happens if the user tries to upload a file whose name matches one of the scripts that live in "bin"?)
So where should you be putting the files?
In my opinion, you've got three choices:
In a subdirectory of the work directory ... which is where Tomcat conventionally puts transitory files such as compiled JSPs.
In a custom subdirectory of the Tomcat installation directory.
In a separate directory somewhere else in the file system.
You shouldn't be dropping then in the webapp directory, because files there are typically blown away when the webapp is redeployed, and because there's a greater risk that uploaded files will interfere with your webapp.
You shouldn't be dropping them in the bin or logs or lib or config directories because of the risk of interference ... and because they are simply not the logical place.
If you want to write files relative to the root of the tomcat installation directory, you can find out what that is by calling System.getProperty("catalina.base").
But what ever you do, you need to make sure that a user can't accidentally or deliberately upload files to the wrong place; e.g by supplying an absolute pathname, or a pathname that uses "../../...." to escape from your upload area.
When you install Apache, the project should be inside the webapp folder :
C:\Apache\tomcat\webapps
Like my Project is gaganisonline so the directory structure is something like this :
Path : C:\Apache\tomcat\webapps\gaganisonline
gaganisonline
| |
WEB-INF index.html
|
---------------------------
| | | |
web.xml src lib classes

Add files in jar during runtime

Project is to create exe file. If we run exe file it will open one admin page (designed in Swing) that page contains browse button, max install, max install sys, and create build, while click the browse button we need to select one exe file from system and that file need to save it inside one folder of jar file. The maxinstall, max install sys value is stored in SQL lite database. This admin page will open for first time only,to get condition from admin. Next if we run the exe file it must check maxinstall and all parameter and then install that selected exe file.
My problem is, I created jar from my java program. While running the jar each time, the admin page only opening (i.e) the database file is not updated inside jar, but its works fine in eclipse. After that I need to create jar to exe.
..it is possible to extract jar in runtime,addfile in the extracted jar and create a new jar in runtime..?
Most JREs will place a file lock on the Jars. Therefore they cannot be updated while the JRE is running. Check a sub-directory of user.home for an altered version of the resource. If it is not found, use the one in the Jar. If it is altered, save the changed data to the sub-dir.
Use a sub-directory based on the package name of the main class, to help avoid overwriting the resources of another app. (or other apps. over-writing your resources).
I think that I understand your problem. Your application stores its state into its own jar file. the fact that you are using SQLite etc. does not matter. It works from Eclipse because in this case the class files and resources are not packaged into archive and your program changes files on file system easily.
The answer is: yes, you can change jar file programmatically. Jar is just a zip. You can use ZipInputStream, ZipOutputStream, JarInputStream, JarOutputStream to modify any zip including your own.
But it is very very not recommended for too many reasons. The right solution is to separate your data from your application. You have to store runtime data on file system, DB etc. For example you can create files in user home directory. It is platform independent. You can also use Preferences class that has portable implementations for all platforms.
Yet another reason to do this is your requirement to create exe. OK, you can change jar file but once you created exe file from your jar you cannot change it anymore.
Once an executable .jar is created it will be "locked". I you add more files to a locked .jar, those files will not be recognized internally by the .jar itself at runtime. So, the better approach is to extract your dependent .jar and then add it to a new, executible .jar containing the new file(s) that you need. Then moving forward you can run this new .jar.

Categories