How can we run .sql file from java without using JDBC? - java

I want to run a .sql file from java without using jdbc on windows 7. I am trying with java runtime class which executes the system commands. But my java is running with only user privileges and the command needs Administrator privileges. The command that am using to run is "sqlplus /nolog #sqlscript.sql".

You need either JDBC, or a 3rd party software+sufficient rights.
No other way. (If we don't count low-level DB access, which would be an overkill)

I was able to do it by installing oracle client as the logged in user(We need to hold the shift key and right click on the exe and select Run as different user). Also we need to set oracle home in non primary partition of the hard diski.e., not where OS is installed. After doing this if i pass a command like "sqlplus /nolog #test.sql" to the Java Runtime class, it executes the script. This works in only win 7 Ultimate and above. Win 7 Home and starter editions does not support this.

Related

How to launch a .jar that needs elevated privileges?

I have a little .jar that executes a simple system administration task and so it needs to be run with elevated privilege. I've been researching this for hours and now know that it can be done in three ways:
1) ran from an elevated cmd prompt
2) convert the .jar to .exe and bundle it with a manifest file
3) use another .jar to launch my .jar and ask for permission.
Option 1) won't work for me because this will need to be deployed to other users that won't know how to do this. Option 2) isn't ideal because I chose to write this app in Java for its portability. This will likely be run on different systems and Java seems the most compatible. So that leaves Option 3) and is where my question comes in. I can't seem to sift through the multitude of info out there on how to accomplish creating a wrapper for my app. With my specs in mind what do you all recommend for creating a wrapper .jar that will prompt the user to allow my .jar to run? Thanks
On Windows, it is possible to run a Java application either as a desktop application, or as a Windows Service in the background. In the case of a Service, the Wrapper needs to be able to be installed, removed, started, stopped, have its status queried, etc. Depending on whether the application has a GUI or is meant to be run in a command window also determines how it will be run.
On Windows systems, the most way of launching the Wrapper is to make use of dedicated batch files to perform each action of controlling the Wrapper. This makes it possible for the end user to double click on the batch file icons or set up links in menus, just have a look if you have the java runtime env.
Nice tutorial: http://wrapper.tanukisoftware.com/doc/english/qna-service.html
Here it has some other possibilities, using Dedicated Batch File, Command Based Batch File or Standalone Binary : http://wrapper.tanukisoftware.com/doc/english/launch-win.html#dedicated
Think you can do this with .bat file. Make sure you have java runtime env, so that you can execute jar file using java -jar command.
If your looking to force the user to use elevated permissions then pure Java isn't going to cut it. I suggest you write native code and use the Java Native Interface (JNI)

Add program to mac osx and windows startup by code or installers?

I have a java system tray application that runs on both windows and mc osx.
I want to add my app to start up programs for both od those os-
which is best:
Check in code which os i'm on and then do a certain action accordingly?
Create a different installer for each os that will add the program to start up?
Also, is there any good example on the best Generic way adding program to windows(for all windows types??) by code or by install, and also a good example for creating a launch agent for the mac?
Thanks
Lior
Update
Initially i would go for #1, as it easier to manage other than managing installation packages for long. but on the other hand, it is not generic, and id like to keep my code generic as possible. And third, i'm on a very short schedule, if there is a 3rd party installers that can warp this up and create different os installer with start up, thats better, under the circumstance of course.
Since you are doing a multi platform Java application I advise you to follow it's 'Write once - run everywhere' rule and either create an self-executable jar file (both Mac and Windows are capable of running those, e.g. explained here) or you could try an out-of-the-box solution like IzPack
For #1: Check the system property os.name on a win 7 box you will get Windows 7. You can query system properties with
System.getProperty("os.name");

Install fonts from a java program

I would like to install a set of fonts to the windows system from my java class. I am using these font for my Birt Report.
You can write a batch/powershell script and include it, along with the font files in your application. Then you can execute the script with
Runtime.getRuntime().exec(...)
You'll most likely have to raise the privileges for your application once you run it.
As for passing the password. It's possible to run cmd.exe so it pops up and propts the user for it. You can also try assigning the return value of exec to a Process class object, which has InputStream and OutputStream properties. I'm not sure how to do it properly. I did it once in a project, a couple of years ago but I no longer have the code.
If you only have to install the fonts once, consider creating an installer for your java application that will take care of it. There's a neat installer generator called IzPack, which allows you to create complex installers using XML. It also allows you to raise privileges for executables run during the installation. This is the way I do such stuff.
You can install those fonts user System -> Fonts if you are in window to test it out.
If you are trying to arrange it with your program, you must start by including them in your java resource file in order to refer to it later on.
Hope it helps~
Without local admin rights it is possible to add custom fonts to the font cache. Then your custom fonts will be available to all your applications until you log out.
The Windows API that does that is AddFontResource. Via a JNI helper DLL you can call it directly, or just execute the RegisterFonts utility.

Java application and MySQL installation in a single package

How can I pack a Java application and MySQL installation files in a single exe file? Can I install the MySQL files automatically in background (or without any inputs from user)? This is just to simplify the installation procedure.
Java is cross platform, MySQL isn't, so you'd have to create various installers for multiple platforms with different MySQL binaries. If you want to include MySQL source code for non Windows systems, then that's another story... so I assume you want just an installation for Windows.
First of all, get an installation software that you'll feel comfortable with. There is a nice list of free and non free installers on Wikipedia.
Second thing, you can do a silent MySQL installation. How it's done is explained here.
But note that doing a silent MySQL installation without user's permission doesn't sound too good to me, since MySQL isn't exactly lightweight software and you might mess up something if a user already has MySQL somewhere installed.
So, by doing this, you have to be extra careful to check if port 3306 is already up and running (default MySQL port), and other sanity checks to see if there's a possibility of another instance lurking in the background.
It would be better if you at least informed your user that MySQL will be installed. Think about these details, because they might be dealbreakers so some of your users.
Use Java Web Start to launch the application.
JWS offers an ExtensionInstallerService that can be used for installing MySQL. Here is a small demo. of the ExtensionInstallerService.

How to restart the Java Process with -Djava.library.path?

I have an application that is started with JWS. The first time user launches this application he has to choose a path where Berkeley DB XML is installed. I do need this to set the native library path and restart the application with -Djava.library.path parameter. Berkeley DB XML java bindings uses JNI to make calls to the database. Since our users may have different OS I cannot rely on a default location.
So, I have a problem with getting current classpath. When I print out "java.class.path" it only gives me "/System/Library/Frameworks/JavaVM.framework/Resources/Deploy.bundle/Contents/Home/lib/deploy.jar". I have three jars that I cannot find in my sys props.
on my Mac.
I hope this was understandable and thanks for any tips beforehand.
Try to repaire permissions with DiskUtil.
Avoid using this pattern. What you could do is to store the command and execute that simulating that you are starting a new process.

Categories