This native Java Agent allows you to change the time in a Java program, without affecting the system clock. It allows control over the System.currentTimeMillis()
method, which is used by most other time-related functionality provided by the JVM.
Use one of these options:
Download libfaketime.jnilib into some location.
OR
Run this, on a clone of this repository:
gcc -shared -I $JAVA_HOME/include -Wall src/FakeTimeAgent.c -o libfaketime.jnilib
OR
Run this, on a clone of this repository:
gcc -fPIC -shared -I $JAVA_HOME/include -I $JAVA_HOME/include/linux -m64 -Wall src/FakeTimeAgent.c -o libfaketime.so
OR
Run this, on a clone of this repository:
gcc -fPIC -shared -I $JAVA_HOME/include -I $JAVA_HOME/include/linux -m32 -Wall src/FakeTimeAgent.c -o libfaketime.so
Run your Java program (say, org.test.Main) with these agent-specific extra arguments (see issue #3), like this:
java -agentpath:/path/to/the/library/you/got/above \
-XX:+UnlockDiagnosticVMOptions \
-XX:DisableIntrinsic=_currentTimeMillis \
-XX:CompileCommand=exclude,java/lang/System.currentTimeMillis \
org.test.Main
In your Java code, you can set the property faketime.offset.seconds to the number of seconds you want the time altered by. For example, to add a day, you can do something like this:
System.setProperty("faketime.offset.seconds", "86400");
That's it! Take a look at FakeTimeTest.java if you need to see some Java code which uses it.