Programming tips

Java

Proxy

System.setProperty("http.proxyHost", "localhost");
System.setProperty("http.proxyPort", "8888");

Find no final static variables

findstr /s static *.java | findstr /v final | findstr /v ( | findstr /v import | findstr /v enum | findstr /v /r "static\ \{" | more

Parse JSON easily

// It uses Jackson jr: https://github.com/FasterXML/jackson-jr
import com.fasterxml.jackson.jr.ob.JSON;
Map map = JSON.std.mapFrom(strJSON);
String foo = (String) map.get("Foo");
List bar = (List) map.get("Bar");
Map baz = (Map) map.get("Baz");
// See also: "Jackson Jr for casual JSON reading/writing from Java" https://medium.com/@cowtowncoder/jackson-jr-for-casual-json-reading-writing-from-java-f5d379d4bcb6#61e8
				

Jakarta EE / Java EE

Inject DataSource

@Resource(lookup = "jdbc/foo")
private javax.sql.DataSource ds;

Do not use @Resource(name = "jdbc/foo") On the reference implementation (Glassfish) it will try to connect to Derby.

Spring

Inject an EJB on Spring

// MyComponent.java

import org.springframework.stereotype.Component;

@Component
public class MyComponent implements MyComponentInterface {

	@EJB(mappedName = "java:module/MyEJB")
	private MyEJB myEJB;

	// ...
}	


// MyEJB.java

import javax.ejb.Stateless;

@Stateless
public class MyEJB {
	// ...
}
				

About java:module, see "32.4.1.1 Portable JNDI Syntax" on the Java EE 7 Tutorial.

HTML

Maven

Executable JAR

	<plugin>
	    <groupId>org.apache.maven.plugins</groupId>
	    <artifactId>maven-jar-plugin</artifactId>
	    <configuration>
		<archive>
		    <manifest>
			<addClasspath>true</addClasspath>
			<classpathPrefix>libs/</classpathPrefix>
			<mainClass>org.foo.bar.Main</mainClass>
		    </manifest>
		</archive>
	    </configuration>
	</plugin>
	<plugin>
	    <groupId>org.apache.maven.plugins</groupId>
	    <artifactId>maven-dependency-plugin</artifactId>
	    <executions>
		<execution>
		    <id>copy-dependencies</id>
		    <phase>prepare-package</phase>
		    <goals>
			<goal>copy-dependencies</goal>
		    </goals>
		    <configuration>
			<outputDirectory>
			    ${project.build.directory}/libs
			</outputDirectory>
		    </configuration>
		</execution>
	    </executions>
	</plugin>
				

Netbeans

Print variable on breakpoint

On Breakpoint properties / Actions section / Print Text, write {=variablename}

SQL Server

TODO

TODO.

TODO.

Computing tips

MacOS / UNIX

Count files recursively, ignore hidden (dot files)

find . -type f -and \( -not -name ".*" \) | wc -l

Move files in direct subdirectories here

for f in *; do cd "$f"; mv * ..; cd ..; done

Unzip all files here

for f in *.zip; do unzip "$f"; done

Microsoft Windows

Hibernate

shutdown /h /t 10 /c "Sleeping..." /d u:0:0

Force update policies

gpupdate /force

Fast copy

ROBOCOPY [from] [to] /E /R:1 /W:1 /NFL

Microsoft Office (Word, Excel, etc.)

Recover unsaved files

File tab / Manage Documents / Manage Document button / Recover Unsaved Documents

%userprofile%\AppData\Local\Microsoft\Office\UnsavedFiles

KeePass

Where are custom password generator stored

%APPDATA%\KeePass\KeePass.config.xml

Source.