On a windows 2012 R2 platform I noticed that winver returns 6.3, but System.getProperty("os.version") returns 6.2 ; I am looking at this source code :
class [More ...] Properties extends Hashtable<Object,Object> {
protected Properties defaults;
public String [More ...] getProperty(String key) {
Object oval = super.get(key);
String sval = (oval instanceof String) ? (String)oval : null;
return ((sval == null) && (defaults != null)) ? defaults.getProperty(key) : sval;
}
}
I am suspecting the value of os.version is obtained from here. Is my suspicion right ?
Object oval = super.get(key);
What would be the contents of the HashTable and how is this populated ? ( I have not loaded the java source code as a project into my eclipse work-bench)
The system property os.version is added by the JVM itself thanks to the static native method initProperties(Properties props) as you can see here at the line 527. This method is called while initializing the System class which is done by the method initializeSystemClass().
In other words it means that the native code of your JVM is not able to recognize your os version, you should upgrade your JDK to fix this issue.
Here is a blog post where the blogger had the same issue with an old version of Java, upgrading it was enough to fix the issue.
This problem was seen before java 6u38. From 6u38, this issue is solved. The security baselines for the Java Runtime Environment (JRE) at the time of the release of JDK 6u38 are specified.
First using the EPM Java JDK version.
As you see it is generating incorrect information, now using a later JDK 7 release.
So this highlights it is the down to the version of Java which is causing the issue.
Resource Link:
EPM 11.1.2.4 - Java versions and why Windows Server 2012 is not
correctly recognised
I am suspecting the value of os.version is obtained from Object oval =
super.get(key);. Is my suspicion right ?
Answer:
You are right. But here is some mechanism
First mechanism:
System.getProperty("os.version"); //which is called the OS version.
The getProperty method returns a string containing the value of the property. If the property does not exist, this version of getProperty returns null.
Second Mechanism:
System.getProperty("os.version", "Windows Server 2012 R2(6.3)");
getProperty requires two String arguments: the first argument is the key to look up and the second argument is a default value to return if the key cannot be found or if it has no value. For example, the following invocation of getProperty looks up the System property called os.version. This is not a valid system property, so instead of returning null, this method returns the default value provided as a second argument: "Windows Server 2012 R2(6.3)"
The last method provided by the System class to access property values is the getProperties method, which returns a Properties object. This object contains a complete set of system property definitions.
What would be the contents of the HashTable and how is this populated
?
Answer:
Properties extends java.util.Hashtable. Some of the methods inherited from Hashtable support the following actions:
testing to see if a particular key or value is in the Properties
object,
getting the current number of key/value pairs,
removing a key and its value,
adding a key/value pair to the Properties list,
enumerating over the values or the keys,
retrieving a value by its key, and
finding out if the Properties object is empty.
You can learn more from here about System Properties and Properties
Property related info is read and can be changed through this java class: PropertiesTest.java
Note and Recommendation from Oracle
Warning: Changing system properties is potentially dangerous and
should be done with discretion. Many system properties are not reread
after start-up and are there for informational purposes. Changing some
properties may have unexpected side-effects.
Note: Some of the methods described above are defined in Hashtable,
and thus accept key and value argument types other than String. Always
use Strings for keys and values, even if the method allows other
types. Also do not invoke Hashtable.set or Hastable.setAll on
Properties objects; always use Properties.setProperty.
Related
I am using Sonarlint with VSCode on a Java project on Windows 10. My project has a variable naming convention such that we use underscores as long as it isn't the first character. These variables are triggering Java Rule S116 "Field names should comply with a naming convention". The doc on this rule says that the default regex it uses is '^[a-z][a-zA-Z0-9]*$' It also says:
Parameters
Following parameter values can be set in the
SonarLint:Rules user settings. In connected mode, server side
configuration overrides local settings.
format Regular expression used to check the field names against.
(Default value: ^[a-z][a-zA-Z0-9]*$)
This strongly implies that the value of the regex can be changed by the user and that it can be done with local configuration. But I can't figure out from this information what exactly do I do to change the value. Any ideas?
In the settings GUI find the setting for this extension and change it to
^[a-z][a-zA-Z0-9_]*$
There wasn't a place in the setting GUI for this, but it can be set in the user (not workspace) settings.json file, by adding this entry:
"sonarlint.rules": {
"java:S116": {
"parameters": {
"format": "^[a-z][a-zA-Z0-9_]*$"
}
}|
}
I'm currently trying to test the UI of an Eclipse RCP application. When executed manually, the application starts fine and can be used correctly. However, when QF-Test launches the application, I get a ClassCastException in a 3pp module:
java.lang.ClassCastException: java.io.File cannot be cast to java.lang.String
at com.solarmetric.conf.ConfigurationImpl.fromProperties(ConfigurationImpl.java:560)
at com.solarmetric.conf.ConfigurationImpl.loadDefaults(ConfigurationImpl.java:186)
After analyzing the code of the 3pp library I see that the exception occurs when trying to cast a System's property value to a String. This shouldn't be a problem because all properties values should be String (see this answer). However, QF-Test is adding 3 properties which their values are File (java.io.File) objects. More precisely:
jython.home = C:\Program Files\qfs\qftest\qftest-4.2.0\jython
groovy.home = C:\Program Files\qfs\qftest\qftest-4.2.0\groovy
javascript.home = C:\Program Files\qfs\qftest\qftest-4.2.0\javascript
I would like to remove those wrong property values. I've already tried to define them manually as parameters of the QF-Test command line call without success.
Some help would be very appreciated.
This behaviour of QF-Test was fixed with QF-Test 4.2.1 (released February 26, 2018), see https://www.qfs.de/en/qf-test-manual/lc/manual-en-history.html#sec_N1D715:
Bug fixed:
In a few cases a broken system property set by QF-Test could interfere with SUT startup.
So the answer is to simply update your QF-Test!
Unfortunately, I do not know a fix for QF-Test.
If possible, I recommend the workaround to correct the properties before use.
Properties sysProps = System.getProperties();
Properties copyProps = new Properties();
synchronized (sysProps) {
copyProps.putAll(sysProps);
}
Set<Entry<Object, Object>> entrySet = copyProps.entrySet();
for (Entry<Object, Object> entry : entrySet) {
if (!(entry.getKey() instanceof String) || !(entry.getValue() instanceof String)) {
sysProps.remove(entry.getKey());
sysProps.put(String.valueOf(entry.getKey()), String.valueOf(entry.getValue()));
}
}
I want to know whether the user launched our Java-based application from a read-only file system like from a .dmg, so functions like auto-update will be able to show meaningful information instead of aborting with an error. I first thought checking the .app's path would be sufficient (when launched from a .dmg it is something like /Volumes/MyApp 1.2.3/MyApp.app, but this won't work, because the user might have installed the application on a different partition. What other things may I check?
You can use -[NSURL getResourceValue:forKey:error:] with the key NSURLVolumeIsReadOnlyKey. You would apply this to the app bundle URL as returned by [[NSBundle mainBundle] bundleURL]. So:
NSBundle* bundle = [NSBundle mainBundle];
NSURL* bundleURL = bundle.bundleURL;
NSNumber* readOnly;
NSError* error;
if ([bundleURL getResourceValue:&readOnly forKey:NSURLVolumeIsReadOnlyKey error:&error])
{
BOOL isReadOnly = [readOnly boolValue];
// act on isReadOnly value
}
else
{
// Handle error
}
If OSX is POSIX compliant, to determine if filesystem is mounted R/O, You can use statvfs() or fstatvfs(), returned struct statvfs field f_flag should have ST_RDONLY bit set for R/O filesystem.
As it was pointed in comments, check if this information is correctly provided by OS.
JNA and this question may be usefull for Java.
A few more ideas, which may be usefull here (access(), open(), utime() ).
OS X specific statfs() may be used too, but this function is not portable (Linux and *BSD have slightly different statfs() functions).
You can also check directly from Java whether a certain path points to something within a read-only directory by querying the FileStore associated with your path:
File classpathRoot = new File(MyClass.class.getClassLoader().getResource("").getPath());
/* getPath() actually returns a String instead of a Path object,
* so we need to take this little detour */
Path yourAppPath = classpathRoot.toPath();
boolean isReadOnly = Files.getFileStore(yourAppPath).isReadOnly();
I loaded default package from infinispan.org version 6.0.0.
Ran standalone.sh on CentOS release 6.3 and trying to access to it from java client using hotrod.
Scenario:
Put key value - there is log on server where said that element was added sucessfully
Get by key - null returned, there is log on server where said that element was not found by this key
GetBulk - returned element
Here is code that I use:
RemoteCacheManager cacheContainer = new RemoteCacheManager(new ConfigurationBuilder().addServer().host(SERVER_HOST).port(SERVER_PORT).build());
RemoteCache<Integer, Integer> cache = cacheContainer.getCache();
Integer result = cache.put(key, value);
Integer value = cache.get(key);
Map<String, String> bulk = cache.getBulk();
The same scenatio works perfectly if infinispan installed on windows.
java version "1.7.0_45" (both, client and server).
The same fault behavior occures no matter remote calls or local ones.
If we call getBulk or keySet - returns data that we expect.
Checked sended and recieved keys - they are equal.
Checked jvm version, tried on different and equal ones.
Tried using different keys and values: String, Integer, Custom objects.
What am I doing wrong?
steps I do:
I do in code
System.setProperty("myproperty", 1);
and then I set in a shell script the property "myProperty" to 3.
like this:
# setprop "myproperty" 3
and then in the code I try to read the property like this:
System.getProperty("myproperty");
I get the value of 1. which means that the set from the shell didn't actually work.
but when I print all props from shell with
# getprop
I see in the list that myproperty equals 3.
in shorter words: I want to change the value of a property from a script, and I see that this scripts actually changes the property but in the java code I get the old value.
any ideas?
Java code in Android provides System.getProperty and System.setProperty functions in java library but it's important to note that although these java APIs are semantically equal to native version, java version store data in a totally different place. Actually, a hashtable is employed by dalvik VM to store properties. So, java properties are separated, it can't get or set native properties, and neither vice versa.
You can use android.os.SystemProperties class can manipulate native properties, though it's intended for internal usage only. It calls through jni into native property library to get/set properties.
getprop/setprop work on android.os.SystemProperties, not on java.lang.System.
Unfortunately, this class is not available to third party application. Apparently you have rooted your device, so you may still access it.
You can use that snippet to run getProp as shell command and get the value of any property:
private String getSystemProperty(String propertyName) {
String propertyValue = "[UNKNOWN]";
try {
Process getPropProcess = Runtime.getRuntime().exec("getprop " + propertyName);
BufferedReader osRes =
new BufferedReader(new InputStreamReader(getPropProcess.getInputStream()));
propertyValue = osRes.readLine();
osRes.close();
} catch (Exception e) {
// Do nothing - can't get property value
}
return propertyValue;
}