• Post Reply Bookmark Topic Watch Topic
  • New Topic

What wrong in this program ???????????  RSS feed

 
priya Agrawal
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;



public class SupportedJavaVersionCheck {


public boolean evaluateRule() {

String javaExe = "";
String versionToCheck = "", version = "";

// get values from IA variables
javaExe ="D:\\program\\Java\\j2sdk1.4.2_06\\bin\\java ";
versionToCheck = "1.4";
System.out.println("javaexe " + javaExe);
System.out.println("versiontotcheck " + versionToCheck);

if (javaExe.equals("") || versionToCheck.equals("")) {
System.out.println("java executable = " + javaExe + "\nversion to check = " + versionToCheck);
return false;
}

try {
version = getJavaVersion(javaExe);
if (versionToCheck.equals(version)) {
return true;
}
else {
return false;
}
}
catch (Exception e) {
System.out.println("unable to determine java version\n" + e.getMessage());
}


return false;
}



private String getJavaVersion(String pJavaExecutable)
throws IOException, InterruptedException {

String javaVersion = "";
String executeString = pJavaExecutable + " -version";

final Process p = Runtime.getRuntime().exec(executeString);

// Intialize StringWriters that capture the output from the command
final StringWriter errWriter = new StringWriter();
final StringWriter outWriter = new StringWriter();

// Launch a thread to capture the Standard error of the Command
Thread errThread = new Thread()
{
public void run()
{
InputStream err = p.getInputStream();
int c;
try
{
while ((c = err.read()) >= 0)
{
errWriter.write(c);
}
}
catch (IOException ioe)
{
}
}
};

// Launch a thread to capture the Standard error of the Command
Thread outThread = new Thread()
{
public void run()
{
InputStream out = p.getInputStream();
int c;
try
{
while ((c = out.read()) >= 0)
{
outWriter.write(c);
}
}
catch (IOException ioe)
{
}
}
};

outThread.start();
errThread.start();
p.waitFor();

// get the information captured from the error StringWriter
String stdErr = errWriter.toString();
String stdOut = outWriter.toString();

// get the return code and return the empty string if exit code is 0 (i.e., the process returned an error)
int exitCode = p.exitValue();
if (exitCode != 0) {
System.out.println("command " + executeString +
" returned error (exit code = " + exitCode + ")");
return "";
}

// get the version number from standard error
System.out.println("execute command:" + executeString + "\nStdOut: " + stdOut +
"\nStdErr: " + stdErr
+ "\nExit Code: " + exitCode);
try {
// parse by white-space, version should be third element in array
String[] output = stdErr.split("\\s");
javaVersion = output[2];
// remove the enclosing quotes
javaVersion = javaVersion.substring(1, javaVersion.length() - 1);
}
catch (Exception e) {
System.out.println("unable to determine java version from command output " + stdErr
+ "\n" + e.getMessage());
}

return javaVersion;
}

public static void main(String [] args)
{
SupportedJavaVersionCheck s = new SupportedJavaVersionCheck();
System.out.println(s.evaluateRule());
}
}
 
Mattias Arthursson
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What are you trying to do? Didn't look closely at the defails, but it looks like you're trying to get the jvm version you are currently running on. A far easier way of doing that would be
 
priya Agrawal
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks,

Definattly this is a good apporch.

ohhhhhhhh But problem is if in my system i set different classpath (path=D:\a\jdk\bin;%PATH%)then it gives version according to setting of classpath variable. But Now i have to create a program that finds the JVM version according to folder path ie (C:\abc\java\jdk1.4\bin). Not dependent on System variable.

Plz help me

thanks,

wait for reply
 
Joe Ess
Bartender
Posts: 9436
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are confusing the PATH environment variable:
Originally posted by priya Agrawal:
(path=D:\a\jdk\bin;%PATH%)

With the CLASSPATH environment variable. The PATH determines which programs are executed without specifying a full path to an executable. The CLASSPATH tells the Java VM where to find class files and resources.
The call to

will always return the version of the currently running VM.
 
Carol Enderlin
drifter
Ranch Hand
Posts: 1364
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't say what your program is or isn't doing that you aren't happy with.

The JavaRanch JavaIntermediateFaq has a link to a great Runtime.exec resource, When Runtime.exec() won't.
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my opinion, the whole idea is wrong.

I once had a program, which tried to make sure, it was running at least on version 1.1 and used some specialized code for that version.
For 1.2 it used other specialized code, and some default code for 1.3.

Well - it refused working with 1.4, just because of those checks.
It came with source-code, and after removing the check for 1.3 it worked flawlessly.

The prove for the pudding is the eating - isn't it?

Your program will not survive a simple update to 1.4.2_07, nor a harddrive migration to "C:\\program\\Java\\j2sdk1.4.2_06\\bin\\java ";
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!