How to install Oracle Java 8 on Ubuntu (in may case, Ubuntu 16.04 LTS)

Procedure to install Oracle Java 8 on Ubuntu

You probably want to confirm first that a version of Java is not already installed. If so, you can do that with the java -version command as shown below…

developer@ubuntu:~$ java -version
The program 'java' can be found in the following packages:
 * default-jre
 * gcj-5-jre-headless
 * openjdk-8-jre-headless
 * gcj-4.8-jre-headless
 * gcj-4.9-jre-headless
 * openjdk-9-jre-headless
Try: sudo apt install <selected package>
developer@ubuntu:~$

Change to root…

sudo su -
(enter password)
apt-get install software-properties-common
add-apt-repository ppa:webupd8team/java
apt-get update
apt-get install oracle-java8-installer

Check if Java is successfully installed

java -version
java version "1.8.0_111"
Java(TM) SE Runtime Environment (build 1.8.0_111-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.111-b14, mixed mode)

Procedure to uninstall

If, for some reason, you want to undo this operation and uninstall Oracle Java 8 from Ubuntu, you can then execute the following:

sudo su -
sudo apt-get remove oracle-java8-installer

 

When I’m not quite sure how to get exactly what I need out of the request URL, I write some stanza that spits out the value of just about every relevant getter method on the javax.servlet.http.HttpServletRequest object. I’ve done this more than once. To be honest… more than twice – probably three or more times. I feel really bad about it, honestly. You know what I’m talking about? It looks something like this:

@RequestMapping(value="/root/**", method = RequestMethod.POST)
public void handlePost(Model model,HttpServletRequest request, HttpServletResponse response) {
 
        LOG.trace(">> handlePost() > /root/{resourcePath}: ");
 
        StringBuilder sb = new StringBuilder();
        sb.append(" [");
        sb.append("\n\t request.getContextPath(): ").append(request.getContextPath());
        sb.append("\n\t request.getLocalAddr(): ").append(request.getLocalAddr());
        sb.append("\n\t request.getLocalName(): ").append(request.getLocalName());
        sb.append("\n\t request.getLocalPort(): ").append(request.getLocalPort());
        sb.append("\n\t request.getPathInfo(): ").append(request.getPathInfo());
        sb.append("\n\t request.getPathTranslated(): ").append(request.getPathTranslated());
        sb.append("\n\t request.getProtocol(): ").append(request.getProtocol());
        sb.append("\n\t request.getRemoteAddr(): ").append(request.getRemoteAddr());
        sb.append("\n\t request.getRemoteHost(): ").append(request.getRemoteHost());
        sb.append("\n\t request.getRemotePort(): ").append(request.getRemotePort());
        sb.append("\n\t request.getRequestURI(): ").append(request.getRequestURI());
        sb.append("\n\t request.getRequestURL(): ").append(request.getRequestURL());
        sb.append("\n\t request.getScheme(): " ).append(request.getScheme());
        sb.append("\n\t request.getServerName(): ").append(request.getServerName());
        sb.append("\n\t request.getServerPort(): ").append(request.getServerPort());
        sb.append("\n\t request.getServletPath(): ").append(request.getServletPath());
        sb.append("\n]");
 
        LOG.trace("-- handlePost() > interesting URL and URI related stuff from request object: " + sb.toString());
 
}

C’mon. You know you’ve done it more than once too, so just stop it with the smug smile.

I’m ashamed and sick of it, so this time, I’m dropping a breadcrumb for my future self by posting the output, which will give me a clue in some future date when my brain is wore to the core like it is tonight. As God is my witness, I will never, ever write this code stanza again. Here’s the output…

 21:07:18.650 [http-bio-8080-exec-3] TRACE c.b.c.controllers.LdpRestController - -- handlePost() &gt; interesting URL and URI related stuff from request object: [
         request.getContextPath(): /myapp
         request.getLocalAddr(): 0:0:0:0:0:0:0:1
         request.getLocalName(): 0:0:0:0:0:0:0:1
         request.getLocalPort(): 8080
         request.getPathInfo(): /root/hello/world.jsp
         request.getPathTranslated(): C:\workspace-c\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\myapp\root\hello\world.jsp
         request.getProtocol(): HTTP/1.1
         request.getRemoteAddr(): 0:0:0:0:0:0:0:1
         request.getRemoteHost(): 0:0:0:0:0:0:0:1
         request.getRemotePort(): 49278
         request.getRequestURI(): /myapp/abc/root/hello/world.jsp
         request.getRequestURL(): http://localhost:8080/myapp/abc/root/hello/world.jsp
         request.getScheme(): http
         request.getServerName(): localhost
         request.getServerPort(): 8080
         request.getServletPath(): /abc
]

 

I’ve always felt stupid for writing a simple Spring controller just to return a view when no controller logic is required. Turns out, I was right; there is a better way. So, okay. I was stupid, but now I know and here it is.

Since Spring 3.0, you can use the following tag in your XML configuration:

<mvc:view-controller path="/" view-name="home"/>

That’s an example of a view-controller definition that forwards to a home page without any custom backing controller. The Spring 3.0 documentation describes the tag as follows:

This tag is a shorcut for defining a ParameterizableViewController that immediately forwards to a view when invoked. Use it in static cases when there is no Java Controller logic to execute before the view generates the response.

Following is an example of the full dispatcher-servlet.xml file that I’m using at the moment. You can see at the very bottom that I’ve mapped two of these. The path ‘/index’ routes to a FreeMarker view I’ve defined in an index.ftl file and the path ‘/about’ routes to another FreeMarker view defined in the file, about.ftl.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
 
 
        <!-- This must remain in place even if there are no uses of the properties loaded in this file because it prepares the properties for use in Java classes. For example: @Value("${ldp.installableDataPath}") private String installableDataPath; -->
        <context:property-placeholder location="classpath:/config.properties" />
 
 
        <!-- Spring will search in the bellow paths controller an services annotations -->
        <context:component-scan base-package="com.base22" />
 
        <!-- The URL mapping definition -->
        <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
 
        <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
 
        <!-- freemarker config -->
        <bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
                <property name="templateLoaderPath" value="/WEB-INF/ftl/" />
        </bean>
 
        <!-- View resolvers can also be configured with ResourceBundles or XML files. If you need different view resolving based on Locale, you have to use the resource bundle resolver. -->
        <bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
                <property name="cache" value="true" />
                <property name="prefix" value="" />
                <property name="suffix" value=".ftl" />
        </bean>
 
        <!-- The following tags are shorcuts for defining a ParameterizableViewController that immediately forwards to a view when invoked. They are used in static cases when there is no Java Controller logic to execute before the view generates the response. -->
        <mvc:view-controller path="/index" view-name="index" />
        <mvc:view-controller path="/about" view-name="about" />
 
</beans>

If you prefer Java code configuration over XML files for Spring, you can do this:

@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {
 
  @Override
  public void addViewControllers(ViewControllerRegistry registry) {
    registry.addViewController("/").setViewName("home");
  }
 
}

 

Did you know there’s a tool that can help you search through JAR files to find Java classes or packages? I’ve been using it for years and I am often surprised that so few Java developers have it in their toolbox. Of course, it’s very handy for resolving those pesky NoClassDefFoundError and ClassNotFoundExceptions, but my favorite use case is to find the location of a class and JAR file on my hard-drive so I can decompile it to understand what some vendor’s proprietary code is actually doing behind the scenes. I know vendors frown on this, but whatever; I’ve got a job to do.

The tool is called jarscan and you can read more about it or download it here.

It’s just a little jar file that executes as a command-line program. On my Windows virtual machines, I put the jar file in C:\JarScanner\

If you’d like to find a Java class called “SystemProperty” somewhere in the “C:\IBM\WebSphere\PortalServer” directory. You would execute the following command:

java -jar C:\JarScanner\jarscan.jar -dir c:\IBM\WebSphere\PortalServer -class SystemProperty

That command would then search through every jar file that it can find in the PortalServer directory, including sub-directories. If it finds the specified class file, it reports the location and name of the Jar file containing the class. You can also search for packages, not just class files.

For Help, type the following command:

java -jar C:\JarScanner\jarscan.jar -help

Shout out to Geoff Yaworski for building this handy tool (there’s a PayPal donate link on his site)