Skip to content
marci543 edited this page Nov 20, 2019 · 12 revisions

Popular Java libraries

There are numerous Java libraries which can spare you a lot of hassle -- it's useful to keep the most popular in mind: http://blog.takipi.com/we-analyzed-60678-libraries-on-github-here-are-the-top-100/

Running JUnit test in a specific order

Since JUnit 4.11, the following works (although it is generally considered bad practice):

@FixMethodOrder(MethodSorters.NAME_ASCENDING)

Running parameterized tests without Object[][] types

Since JUnit 4.12, you can run parameterized tests without the "classic" Object[][] types: https://github.com/junit-team/junit4/wiki/Parameterized-tests#tests-with-single-parameter

@RunWith(Parameterized.class)
public class MyTest {

	@Parameters
	public static Iterable<? extends Object> data() {
		return Arrays.asList("A", "B", "C");
	}

	@Parameter
	public String variant;

	// use the `variant` variable...
}

Comparing integers

int vs. long

Long l = 1L;
Integer i = 1;
System.out.println(l.equals(i)); // false
System.out.println(1 == 1L); // true

Outputs:

false
true

See also http://cubussapiens.hu/2012/05/java-primitive-type-comparison-a-wat-look/.

Log4j

Add a log4j.properties file. For Maven projects, it should be in the src/main/resources directory:

log4j.rootLogger=debug, stdout, R

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

# Pattern to output the caller's file name and line number.
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n

log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=example.log

log4j.appender.R.MaxFileSize=100KB
# Keep one backup file
log4j.appender.R.MaxBackupIndex=1

log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n

Only display errors: http://stackoverflow.com/questions/17442877/how-to-configure-log4j-to-log-only-info-messages-and-higher

# Root logger option
log4j.rootLogger=ERROR, stdout

# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

Troubleshooting

  • Problem:

    Exception in thread "main" java.lang.NoClassDefFoundError: java/util/function/Consumer
    [...]
    Caused by: java.lang.ClassNotFoundException: java.util.function.Consumer
    [...]
    
  • Solution: Change the JDK compliance and the JDK to 1.8.

Running Java apps on a high-DPI display in Windows 10

Legacy Java apps (e.g. Mondrian) might appear tiny on modern high-DPI displays.

Detailed answers: https://superuser.com/questions/988379/how-do-i-run-java-apps-upscaled-on-a-high-dpi-display

In essence, you should right click the executable, go to Properties, Compatibility, Override high DPI scaling behavior and pick the System for the Scaling performed by option.

Depending on the packaging of the application, there are two cases:

  1. If the app is bundled as a standalone exe file, just update the compatibility settings of the application.

  2. If the app is distributed as a JAR file, you could use a flag (which doesn't work with Java 8) or you'd need to edit the javaw.exe runtime (e.g. with ResourceTuner). As a workaround, you can simply create a copy of javaw.exe (e.g. javaw2.exe), edit its compatibility settings and use it as a runtime for your application. You're likely need to specify the full path:

"C:\Program Files\Java\your-jre-or-jdk\bin\javaw2" -jar some-app.jar
Clone this wiki locally