Windows7 Batch file: Net Use command - java

I have a team of users that have read only access to a shared network drive. Sometimes these users will need to deploy their project resources to the drive. I am trying to come up with a secure build process for them to use. Currently I am using a batch file that they can execute from their local system which will do the following...
User starts batch file
Batch file calls a java program (the credentials are 'hidden' and 'encrypted' within the java program)
The java program handles the encryption process and then calls a final batch file that actually runs the NET USE command to map the drive with admin credentials
The final batch file maps the drive, copies the required resources onto the shared drive, and then re-maps the drive with original user credentials (read only).
My major problem is that users will have direct access to the batch files that do this entire process and they could simply remove the #ECHO off command from the final batch file to display all the credentials to the cmd output window.
I'm not sure if there's a better solution to this sort of thing? Any ideas will be greatly appreciated!
Also, all machines are using Windows 7 and using a Windows network drive.

The best solution would be to copy the resources directly in the Java program using the jCIFS library.
A second option would be to map the drive from within the Java program. There's more information in this SO question: How can I mount a windows drive in Java?

There are some .bat to .exe compliers out there. Not sure how well they will work for your particular batch file, but probably worth a look. You can search for them. Here's a couple of them out there
Advanced BAT to EXE Complier
Quick Batch File Compiler
Batch File Complier PE

Related

Can GCP cloud function access (write) to /src/main/resources?

I have a cloud function that triggers a jar, it start well until I try to write files in /src/main/resources, where I can read, in the moment that I try to write the program fails and throw an exception.
So can I write directly to /src/main/resources or I must to implement the Cloud Storage library?
References:
https://cloud.google.com/storage/docs/reference/libraries#client-libraries-install-java
https://cloud.google.com/appengine/docs/standard/java11/using-cloud-storage
How to read a file from maven resources folder in GCP Cloud Function?
Thanks to all.
No java code (regardless of platform) can write to that 'directory', in the sense that the point of that dir is that these files end up in the same place that your jar files end up, and that you read them using MyClass.class.getResource. If you ever write new File("src/...") you messed up ('src'? In production there is no 'src', obviously).
That abstraction (MyClass.class.getResource and friends) offer, intentionally, no way to write anything, because jar files are not meant to be writable.
The right place to store mutable data on disk is nowhere near your jar files; for end user systems, generally write in System.getProperty("user.home") and then from there a subdir, such as .yourapp, or ask the user which data directory is appropriate; different OSes have different 'flavours' (linux wants /var or ~, apple wants ~/Library/Application Support or /Library/Application Support, windows is all over the place but probably wants HOME/My Documents, etcetera), and the only one that is common amongst all of them is 'some subdir off of the user's home directory'.
For cloud in particular, you must implement the Cloud Storage Library; the actual underlying OS system your google cloud app runs on is epheremeral (it can just disappear on you and its resources are tossed into the void, if your app is written such that this is a bad thing, your app is broken).

Open a file with a java application

I made a text editor in java. It has a FileExplorer class which allows me to read and write to a file. Now I would like to know how I could open said file (text.txt) with my editor application from outside my application. Basically when I double-click on the file (text.txt) it should start my application and pass some variables(like name and path). The application is a .jar file.
I am not going to bother anyone to go through the 2k lines of code, so I won't post it in here. But it is just a JTextPane in a JFrame and a PrintWriter/BufferedReader reading and writing to the file.
PS: should preferably work cross-platform.
Thanks
This question is not about java. It is about mapping files to specific application using you OS tools.
If you are on Windows you have to map extension *.txt to your application. Take a look on this article for details.
Please note that your application must accept file path in command line.
To make association easier I'd recommend you to writer batch file that runs your application and also accepts file name in command line. Then you just have to associate your batch file with *.txt extension.
If you are on Linux association technique depends on your flavor, but you can google it. Obviously you will have to create shell script instead of batch file.
EDIT
Actually your question is mostly about installation process. There are a lot of installation tools that can do this work for you. Some of the tools are even cross platform (I can remember "install anywhere"). There are both commercial and free tools that do this.

Moving web application (Java/JSP) to a web server

I have developed an application with Netbeans (Using Java, JSP and JQuery) in Windows environment. Now I am ready to transfer the application to a web host so that the application can be available on the Web and I am told that the application would have to be moved to a linux environment (hosting service already bought). Here are my concerns:
How to convert my code to Linux? Is there an automatic tool for this?
How to deploy my application to the server online (what do I need to copy and to what directory on the web?)
My application writes to a directory on c:drive on my laptop, what should I do to make the application write to correct directory a designated directory on the web server?
I have read here and there online but just haven't got specific solutions to this.
How to convert my code to Linux? Is there an automatic tool for this?
One of the Java key features is portability, so as far as you haven't used any OS-specific code like running a program using CMD or similar or a library that is OS-dependant (which is rare in these times but there are some yet), then you don't have anything to do.
How to deploy my application to the server online (what do I need to copy and to what directory on the web?)
You need to generate a WAR file. This file will zip all your web pages (JSPs) and web resources (js, css, image files) along with the Java binaries (*.class) and libraries (that must be on WEB-INF/lib folder).
Since you're working with NetBeans, here's a Q/A to generate the war file: How can I create a war file of my project in NetBeans?
This war file must be placed in the deploy folder of your web application server. Usually, the hosting provides you the tools (generally a link on the web with user and password) to administrate the host, based on this you should ask (or find it by yourself) the option to upload the war file.
My application writes to a directory on c:drive on my laptop, what should I do to make the application write to correct directory a designated directory on the web server?
You need to configure this path as a constant in your application, or even better, configure it in a properties file (or somewhere else) in order to be read and use it by your application easily. Remember that the path should be generic to be supported in almost every OS. For example, if you use a path with name
C:\some\path\for\files
Its generic form will be:
/some/path/for/files
Since you're going to work on Linux, make sure the user who executes the Tomcat (or the web application server you will use on production) have enough permissions to write on that folder. This can be easily done (and somebody here can fix this please) using the chown command:
#> chown -R user /some/path/for/files
Here's another Q/A to manage files and path on Java web applications: How to provide relative path in File class to upload any file?
OK, first a few thoughts:
Convert code to Linux. Once you have your ear of war file, you can just deploy them. It's best if you use UTF8 enconding in your files, specially if you use special characters, but that would be an issue you could test out when you deploy, could also be dependant on the Linux configuration. Having that said, Java is portable and you only have to be sure that the archive you create is compatible with the AppServer that's installed on the Linux hosting. You should get all the information you need about the deployment environment from the hosting site / company.
Deployment will depend from site to site, they should give you all instructions.
Here you might have a problem. I would say that the easiest way is to just map the directory in a properties file and customize it on every machine you use it. That's the easy part so far. However, you should check if your site will give you access to a directory, and be aware of space limitations and cleanup of the files. If you get, let's say, 100MB and you use 10MB a day, you might end up with trouble after 10 days...

protect data from extraction from setup.exe or from jar

I am making the setup of java swing application by using Inno Setup as an exe i am selecting the jar file of my project, I am also adding other necessary resources as folder.
When I am installing the setup on the client side. it is putting the jar and other
resources in program files folder but there client can extract the my java classes
and other resources from jar. I want that client can only use the resources by
application program but he could not extract the resources. How is it possible?
There is literally nothing you can do to entirely prevent someone from extracting the resources.
The best you can do is to make the process a bit difficult; e.g. by storing the resources in the JAR file in encrypted form. The problem is that your program would need to decrypt the resources in order to use them. Someone with sufficient skills and patience can reverse engineer your decryption code and capture the unencrypted resources.
By the way, this is not a Java-specific problem. Any application that you provide to a user as an executable can be reverse engineered ... assuming that the user has the wherewithal to run it in the first place.
The bottom line is that if you are not prepared for the possibility that someone might extract the resources, you should not distribute the executable.

Copy Files Between Windows Servers with Java?

I'm looking for code, or a library, which I can use to copy files between Windows servers using Java. Picture a primary server that runs a background thread, so that whenever a transaction is completed, it backs up the database files to the backup server. (This is required protocol, so no need to discuss the pros/cons of this action). In other words, transaction completes, Java code gets executed which copies one directory to the back-up server.
The way the Windows machines are set up, the primary server has the back-up server's C: drive mapped as it's own Z: drive. Both machines running Windows 2003 or 2008 Server. Java 1.6.
Found the correct answer on another forum and from messing around a little with the settings. The problem with copying files from one machine to another in Windows (using either a .bat file or using straight-up Java code) is the user permissions. On the primary server, you MUST set the Tomcat process to run as the administrator, using that administrator's username and password. (Right-click on the Tomcat service, select "Log On" tab, enter administrator's username/password). The default user that Tomcat runs on (local user), isn't sufficient to copy files between networked drives on Windows. When I set that correctly, both the .bat file solution I had tried previous to this post, and a straight-Java solution suggested here worked just fine.
Hope that helps someone else, and thanks for the suggestions.
Obtain the files by File#listFiles() on a directory from one disk, iterate over each of them, create a new File on the other disk, read from a FileInputStream from the file from one disk and write it to a FileOutputStream on the file on the other disk.
In a nut:
for (File file : new File("C:/path/to/files").listFiles()) {
if (!file.isDirectory()) {
File destination = new File("Z:/path/to/files", file.getName());
// Do the usual read InputStream, write OutputStream job here.
}
}
See also:
Java IO tutorial
By the way, if you were using Java 7, you would have used Path#copyTo() for this.
Paths.get("C:/path/to/files").copyTo(Paths.get("Z:/path/to/files"));
I would really recommend using Apache Commons IO for this.
The FileUtils class provides methods to iterate over directories and copy files from one directory to another. You don't have to worry about reading and writing files yourself because it's all done by the library for you.

Categories