The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com



High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
how to execute java in specified time- urgent radhnki SUN Solaris 1 01-07-2009 06:49 AM
How to execute java program from perl umen Shell Programming and Scripting 1 08-10-2008 03:08 AM
execute unix command from java eclips katzs500 High Level Programming 0 08-05-2008 05:30 AM
How can I get an if statement to execute based on number of lines in a file? LordJezo Shell Programming and Scripting 6 05-14-2004 10:50 AM
execute command unix with java:possible or not? mips Shell Programming and Scripting 4 05-13-2004 07:07 AM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 06-18-2009
smartgupta smartgupta is offline
Registered User
  
 

Join Date: Sep 2008
Posts: 109
Not able to execute the method based on OS in JAVA

Hi,

In my application,I want to find out the OS name and based on OS I want to do some specific operations.So I write some code based on it in JAVA but I am able to get the OS name but Not able to use the method specific to OS.I write a simple code to check it and found that it is not going to the if statement.code is as follow--

Quote:

public class OSnane {

public static void main(String[] args) {

String OsName=null;
OsName=System.getProperty("os.name");

System.out.println("OS name="+OsName);
if(OsName == "SunOS")
System.out.println("hello Sun");
else if(OsName == "Windows XP")
System.out.println("Hello Windows");
else if(OsName == "Linux")
System.out.println("Hello Linux");
else
System.out.println("No OS selected!!!");


}

}
and the Output is:

Quote:
OS name=SunOS
No OS selected!!!

So please tell me how can I Use methods based on OS.
Thanks in Advance ...
  #2 (permalink)  
Old 06-18-2009
cbkihong cbkihong is offline Forum Advisor  
Advisor
  
 

Join Date: Sep 2002
Location: Hong Kong, China
Posts: 1,624
Please consider taking some time for some readings to consolidate your Java knowledge. You don't use equality (==) for string equality tests. You must use the String.equals() method.

e.g. osName.equals("SunOS")

By using == you were comparing two object references and expectedly the equality is false.
Sponsored Links
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 12:21 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0