Obtain version information from prior installation - java

Running the "Update current installation" option, how do I obtain the version info from the prior installation? I've been through the API and the closest thing I've seen is the public static interface ApplicationRegistry.ApplicationInfo.
Edit
This is how I am currently going about it. It works but am not sure if this is the most feasible method.
import com.install4j.api.ApplicationRegistry;
ApplicationRegistry.ApplicationInfo[] AppInfo = ApplicationRegistry.getApplicationInfoById(context.getApplicationId());
return AppInfo[0].getVersion();

You could use the
static ApplicationRegistry.ApplicationInfo getApplicationInfoByDir(java.io.File dir)
Checks if the specified directory contains an application installed by install4j and retrieves information about it.
This will return a ApplicationInfo instead of the ApplicationInfo[].

As an example, the following script checks if the same version is already installed:
// The value returned by context.getInstallationDirectory() will be
// the last installation directory if the user has already installed the application
ApplicationRegistry.ApplicationInfo applicationInfo =
ApplicationRegistry.getApplicationInfoByDir(context.getInstallationDirectory());
if (applicationInfo == null) {
// The application has never been installed before
return true;
}
// The version of this installer is contained in a system installer variable
String myVersion = (String)context.getVariable("sys.version");
if (applicationInfo.getVersion().equals(myVersion)) {
// In that case the current version is already installed.
Util.showErrorMessage("The current version is already installed in this directory");
// By returning "false", the action will fail and the installer will quit.
// Note that you have to set the "Failure strategy" property of your
// "Run script" action to "Quit on error", otherwise the installer will continue.
return false;
} else {
return true;
}
This could for example be used in a "Run script" action in the "Startup" node of the installer.

Related

Multiple java version usage in pipeline jobs

We have upgraded the ant jar files to 51 which needs to detect the java version11 but our old applications are still using the old jar file50 so we need to detect the java versions according to the pull request id. here we give PR id as parameter.
stage("abc"){
steps{
script{
if (PR_ID == '100')
tools{
jdk 'java11.0.5'
}
else
tools{
jdk 'java8'
fi
}
i am trying this way but getting error like java.lang.NoSuchMethodError: No such DSL method 'tools' found among steps [ArtifactoryGradleBuild, MavenDescriptorStep, addInteractivePromotion, archive, artifactoryDistributeBuild, artifactoryDownload, artifactoryEditProps.
Kindly help me on how can we use if else condition or any other condition to detect the java version according to the PR id.
A tools block is only allowed inside a pipeline block or stage block, not inside a script block.
What I would suggest is creating two different stages and using when to select the one to execute.
stage("abc-java11") {
when {
expression {
return PR_ID == '100'
}
}
tools {
jdk 'java11.0.5'
}
steps {
// Your steps
}
}
stage("abc-java8") {
when {
expression {
return PR_ID != '100'
}
}
tools {
jdk 'java8'
}
steps {
// Your steps
}
}

Java check if program is installed on windows

Is there a way to check if a specific program is installed on Windows using Java?
I'm trying to develop a Java program that automatically creates zip archives by using the code line command from 7-Zip.
So, I would like to check in Java if on my windows OS '7-Zip' is already installed. No check for running apps or if OS is Windows or Linux. I want to get a bool (true/false) if '7-Zip' is installed on Windows.
The library Apache Commons has a class called SystemUtils - full documentation is available at https://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/SystemUtils.html.
In this library you have the following static boolean properties at your disposal:
SystemUtils.IS_OS_LINUX
SystemUtils.IS_OS_WINDOWS
The unix-like solution would be to simply try to run the program with --version flag (on windows probably the /? or - like in the 7zip case - without any at all) and check whether it fails, or what the return code will be.
Something like:
public boolean is7zipInstalled() {
try {
Process process = Runtime.getRuntime().exec("7zip.exe");
int code = process.waitFor();
return code == 0;
} catch (Exception e) {
return false;
}
}
I assume that you're talking about Windows. As Java is intended to be a platform-independent language and the way how to determine it differs per platform, there's no standard Java API to check that. You can however do it with help of JNI calls on a DLL which crawls the Windows registry. You can then just check if the registry key associated with the software is present in the registry. There's a 3rd party Java API with which you can crawl the Windows registry: jRegistryKey.
Here's an SSCCE with help of jRegistryKey:
package com.stackoverflow.q2439984;
import java.io.File;
import java.util.Iterator;
import ca.beq.util.win32.registry.RegistryKey;
import ca.beq.util.win32.registry.RootKey;
public class Test {
public static void main(String... args) throws Exception {
RegistryKey.initialize(Test.class.getResource("jRegistryKey.dll").getFile());
RegistryKey key = new RegistryKey(RootKey.HKLM, "Software\\Mozilla");
for (Iterator<RegistryKey> subkeys = key.subkeys(); subkeys.hasNext();) {
RegistryKey subkey = subkeys.next();
System.out.println(subkey.getName()); // You need to check here if there's anything which matches "Mozilla FireFox".
}
}
}
If you however intend to have a platformindependent application, then you'll also have to take into account the Linux/UNIX/Mac/Solaris/etc. (in other words: anywhere where Java is able to run) ways to detect whether FF is installed. Else you'll have to distribute it as a Windows-only application and do a System#exit() along with a warning whenever System.getProperty("os.name") is not Windows.
Sorry, I don't know how to detect in other platforms whether FF is installed or not, so don't expect an answer from me for that ;)

Fixing JRE error in Java code and recompiling application

I tried run on Windows 7 some old utility that depends on old JRE version.
I have last Java Runtime Environment 1.7.0_79 installed. When I attepmt to start application, I got an error:
"Sivus requires JRE 1.4 or later to run. You can download JRE
from...etc"
Is there a workaround to resolve/fix this or something of that nature?
Program main executable packed in SFX ZIP archive, I extracted files, and found Java file, which makes the check. There is a code:
public static boolean checkJavaVersion(){
boolean ok = false;
String version = System.getProperty("java.version");
if (version.indexOf("1.1") > -1) {
ok = false;
}
else if (version.indexOf("1.2") > -1) {
ok = false;
}
else if (version.indexOf("1.3") > -1) {
ok = false;
}
else if (version.indexOf("1.4") > -1) {
ok = true;
}
else if (version.indexOf("1.5") > -1) {
ok = true;
}
return ok;
}
public static void main(String[] args) {
boolean DEBUG = false;
try {
// check if JRE is over 1.4
if (checkJavaVersion() == false){
JOptionPane.showMessageDialog(
null,
"SiVuS requires JRE 1.4 (or later) to run.\n"
+"You can download the latest JRE from java.sun.com",
"Java Run Time Environment Error",
JOptionPane.WARNING_MESSAGE);
System.exit(1);
}
How to correct this issue and recompile the application again? Will the application that relies on JRE 1.4 work with current JRE?
An example of how I'm doing patches like this.
Fix code - you can add 1.6, 1.7, 1.8 to checkJavaVersion or remove this check at main method.
Put fixed java file in folder that matches package
Compile java file, for example:
call "C:\Program Files (x86)\Java\jdk1.7.0_79\bin\javac.exe" C:\test\mypackage\MyClass.java -cp C:\test\My.jar -source 1.4 -target 1.4
Get compiled class file and replace old file (backup it first)
First of all, I would recommend not using an old tool whose "provenance" is doubtful. Especially, not as a security scanner. Surely, you can find a tool that is newer, and better supported than this one ...
If you wish to proceed (and take a risk) then one approach would be to modify that class, recompile and rebuild the JAR file, as suggested by #Rustam.
Another approach would be to create a simple wrapper class with a main method that called System.setProperty to tweak the value of the "java.version" property, and then called the real main method.

Installing Java with silent install into a directory with spaces

I am trying to install Java using the silent mode and also specify an installation directory that contains spaces. When I do this it pops up the "Windows Installer" dialog box indicating one of the parameters is incorrect. If I use the short path name it works correctly, but I really would prefer not to use the short directory name because that is the value that gets stored in the Registry.
The command I want to use...
jre-6u39-windows-i586.exe /s INSTALLDIR="C:\Program Files (x86)\Java"
This pops up the Windows Installer dialog box.
When I use...
jre-6u39-windows-i586.exe /s INSTALLDIR=C:\Progra~2\Java
This works.
NOTE: "Program Files (x86)" is just an example. This is installed at client sites and they choose the install directory, therefore we have to be able to support any directory they may specify.
Any idea how I can do a silent install but still use the long path name?
UPDATE:
I thought I would share the final solution. One cool thing I found that I wanted to share is that you can suppress the auto-reboot of install and it returns an exit code of 3010. Therefore you can defer the reboot to another time. Here is the code (rewritten a bit to eliminate a bunch of our own abstraction)
public bool InstallJava(string installPath, string logFile)
{
bool rebootRequired = false;
string fullLogFileName = Path.Combine(logFile, "JavaInstall.log");
string arguments = string.Format("/s /v\"/qn REBOOT=Suppress INSTALLDIR=\\\"{0}\\\" STATIC=1 /L \\\"{1}\\\"\"", installPath, fullLogFileName);
ProcessStartInfo startInfo = new ProcessStartInfo { RedirectStandardError = true, RedirectStandardOutput = true, RedirectStandardInput = true, UseShellExecute = false, CreateNoWindow = true,
FileName = "jre-7u25-windows-x64.exe", Arguments = arguments };
var process = Process.Start(startInfo);
process.WaitForExit();
if (process.ExitCode == 3010)
rebootRequired = true;
else if (process.ExitCode != 0)
{
// This just looks through the list of error codes and returns the appropriate message
string expandedMessage = ExpandExitCode(StringResources.JAVA_INSTALL_ERROR, process.ExitCode, fullLogFileName);
throw new Exception(expandedMessage);
}
return rebootRequired;
}
i recall encountering this issue before....
You need to use quotes when passing paths to the installer if the
paths have spaces. Because the path arg is already in quotes, you
need to escape each quote with a '\' so it gets passed through. So
the command would be
j2re.exe /s /v"/qn INSTALLDIR=\"C:\Program Files\JRE\""
reference :
http://docs.oracle.com/javase/1.5.0/docs/guide/deployment/deployment-guide/silent.html
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4966488

How do I set the MONGOHQ_URL environment variable in the run configuration of Netbeans?

We're working on deploying a Java project to Heroku that uses MongoDB. According to the Heroku docs, the DB connection parameters are read from an environment variable, MONGOHQ_URL. When I run the project in Netbeans on my laptop, how do I set this variable?
I tried adding it as a VM option with -DMONGOHQ_URL=... in Run -> Set Project Configuration -> Customize -> Run and as well in Actions -> Run project and Run file via main(), but to no avail. When the program reads it with System.getvar it's not set.
You can set it in your netbeans.conf file. Add the line:
export MONGOHQ_URL=...
There's an example here: http://sunng.info/blog/2009/12/setting-environment-variables-for-netbeans/.
Ok, I figured it out. This may be obvious to Java coders, but I'm not one, so here is what I cobbled together.
String mongo_url = System.getenv("MONGOHQ_URL");
// If env var not set, try reading from Java "system properties"
if (mongo_url == null) {
mongo_url = System.getProperty("MONGOHQ_URL");
}
MongoURI mongoURI = new MongoURI(mongo_url);
this.db = mongoURI.connectDB();
// Only authenticate if username or password provided
if (!"".equals(mongoURI.getUsername()) || mongoURI.getPassword().length > 0) {
Boolean success = this.db.authenticate(mongoURI.getUsername(), mongoURI.getPassword());
if (!success) {
System.out.println("MongoDB Authentication failed");
return;
}
}
this.my_collection = db.getCollection("my_collection");

Categories

Resources