DEV Community

Cover image for How to make the best of a slow machine running on limited resources with a Windows environment as a Java Engineer
João Esperancinha
João Esperancinha

Posted on

How to make the best of a slow machine running on limited resources with a Windows environment as a Java Engineer

So this is the question. Let's think about this.

When we get into the job market, not always we get the best machines to work with and to make the things we love. Having a Windows machine isn't necessarily bad news, but the machines that usually come with it, either have limitations or you end up needing more tools than the ones that Windows seems to offer. Nonetheless, many things are actually very much possible with a Windows machine. They usually just have a different shape.

I personally have more preference for Linux and Mac-OS based machines. But that's also because I don't do much professional work in C# or other .Net technologies.

This first post is a celebration of the all the things that I have learned that have gotten me through working with Windows machines through the years, in those odd occasions.


1. Environment Setup

1.1. Chocolatey

If you come from a world where you are used to using package managers, you will probably want to keep using such a mechanism. For this you can install Chocolatey

  1. Run Windows PowerShell with administrative rights.

blognewline

  1. The following is on the website itself. I'm putting it here, just to summarise it:

Run

Get-ExecutionPolicy
Enter fullscreen mode Exit fullscreen mode

If it returns Restricted, then run

Set-ExecutionPolicy AllSigned
Enter fullscreen mode Exit fullscreen mode

or

Set-ExecutionPolicy Bypass -Scope Process
Enter fullscreen mode Exit fullscreen mode

, in Installing Chocolatey

NOTE: If you end up changing policy, you need to restart your computer

  1. Run the installation command:

Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))

1.2. Install necessary software

choco install -y minikube
choco install -y virtualbox
choco install -y maven
choco install -y git
choco install -y vim
Enter fullscreen mode Exit fullscreen mode

2. Install Docker

  • There are multiple ways to install docker:
  1. Docker. This is my favorite. It is very straightforward and you get docker running in no time. However, for one or another reason, you may find it difficult to do so.

  2. Chocolatey. Using the command line with Chocolatey:

choco install -y docker
choco install -y docker-machine
choco install -y docker-desktop
docker-machine create default
docker-machine start
Enter fullscreen mode Exit fullscreen mode

After this last create command, there will be an output to run an extra command. This may differ by machine and environment. Please run that.
Here is an example:

docker-machine.exe env default | Invoke-Expression
Enter fullscreen mode Exit fullscreen mode

Essentially what this does is that it binds your command line to the docker environment. In this case it is default.


3. Installing Ubuntu for windows

  • In order to install Ubuntu in your windows machine, there are two options.

Install Ubuntu for Windows (pre-requisite: at least Windows 10 Professional)

To enter this environment, please use the BASH command

bash.exe
Enter fullscreen mode Exit fullscreen mode

If for some reason, all of the above fails, then you can try running a container in your machine:

docker run -v /c/Users/jofis:/root -v /c/Users/jofis/jvm:/usr/lib/jvm --rm --name container-developer -td ubuntu
Enter fullscreen mode Exit fullscreen mode

you can now use your docker machine to build your project. The volume mappings ensure that your home folder is shared and so will be .m2 and .sdkman (should you use it), with it. They also ensure that the jvms are shared in order to be used with intellij.

To enter this environment, please go into your container:

docker exec -it container-developer bash
Enter fullscreen mode Exit fullscreen mode

NOTE: 🔴 Remember to be careful not to remove this container accidentally


4. Installing Java in Ubuntu for Windows

Once you are in your environment, please choose one of the following sequence of commands:

4.1 SDK-MAN. Via SdkMan!

curl -s "https://get.sdkman.io" | bash
source "$HOME/.sdkman/bin/sdkman-init.sh"
sdk install java 8.0.242.hs-adpt
sdk install java 11.0.6.hs-adpt
sdk install java 12.0.2.hs-adpt
sdk install java 13.0.2.hs-adpt
sdk install java 14.0.0.hs-adpt
Enter fullscreen mode Exit fullscreen mode
  • If you'd like you can add this to you .bashrc. This contains aliases that you can use to quickly switch betweek Java SDKs.
alias java8="sdk use java 8.0.242.hs-adpt"
alias java11="sdk use java 11.0.6.hs-adpt"
alias java12="sdk use java 12.0.2.hs-adpt"
alias java13="sdk use java 13.0.2.hs-adpt"
alias java14="sdk use java 14.0.0.hs-adpt"
alias m2disable="rm ~/.m2/settings.xml"
alias m2enable="cp /your_repo_folder/settings.xml ~/.m2/"
Enter fullscreen mode Exit fullscreen mode

4.2. Ubuntu. Via Ubuntu commandline with APT

apt-get -y update
apt-get -y upgrade
apt -y install apt-transport-https ca-certificates wget dirmngr gnupg software-properties-common
wget -qO - https://adoptopenjdk.jfrog.io/adoptopenjdk/api/gpg/key/public | apt-key add -
add-apt-repository --yes https://adoptopenjdk.jfrog.io/adoptopenjdk/deb/
apt -y update
apt -y install openjdk-11-jdk
apt install openjdk-13-jdk
apt -y install adoptopenjdk-8-hotspot
apt -y autoremove
Enter fullscreen mode Exit fullscreen mode
  • If you'd like you can add this to you .bashrc. This contains aliases that you can use to quickly switch betweek Java SDKs.
alias java8="export JAVA_HOME=/usr/lib/jvm/adoptopenjdk-8-hotspot-amd64 && update-java-alternatives -s adoptopenjdk-8-hotspot-amd64"
alias java11="export JAVA_HOME=/usr/lib/jvm/java-1.11.0-openjdk-amd64 && update-java-alternatives -s java-1.11.0-openjdk-amd64"
if [[ -d /usr/lib/jvm/java-13-oracle ]]
then
    alias java13="export JAVA_HOME=/usr/lib/jvm/java-13-oracle && update-java-alternatives -s java-13-oracle"
elif [[ -d /usr/lib/jvm/java-1.13.0-openjdk-amd64 ]]
then
    alias java13="export JAVA_HOME=/usr/lib/jvm/java-1.13.0-openjdk-amd64 && update-java-alternatives -s java-1.13.0-openjdk-amd64"
else
    echo "Java13 not found!"
fi
alias m2disable="rm ~/.m2/settings.xml"
alias m2enable="cp /your_repo_folder/settings.xml ~/.m2/"
Enter fullscreen mode Exit fullscreen mode

NOTE: the m2disable and the m2enable aliases are there so that you can easily switch between a possible settings.xml file and not having any installed.

4.3. Install utilities

apt install -y vim
apt install -y git
apt install -y git-gui
apt install -y maven
Enter fullscreen mode Exit fullscreen mode

5. Intellij

Let's finally look at how can we use intellij with these command lines. If you picked up the container solution, please make sure that your container is running. We have already seen above how to log in it.

For this example we are going to look at the most complicated example. This is the one that does not use SDKMAN to get the different Java SDK's

Since we are sharing the jvm folder, we can just configure Intellij to use Java 11 for example:

alt text

Now let's search for Terminal in Preferences/Settings:

  • This our initial setting in a Windows environment. The cmd command line:

alt text

  • We can choose bash, if we have installed Ubuntu for Windows:

alt text

  • We can choose powershell if we don't have Ubuntu for Windows installed. This is the one we pick for our example:

alt text

  • If we run the usual commands to get into the docker enviroment and as a recap on my example these are:
docker-machine.exe env default | Invoke-Expression
docker exec -it container-developer bash
Enter fullscreen mode Exit fullscreen mode

We then get something like this:

alt text

This is it, these are just some examples on how we can get a machine to work for us.


6. Conclusion

I deliberately did not use GIT for windows, because in this setting we are using an ubuntu command line. This means that we have much more possibilities. Also, the MinGW provided by this installation, needs quite a lot of fine-tuning before it gets reasonably functional.

We are always way better off, if we can get the Windows Subsystem for Linux to work. Unfortunately that is not the case for Windows Home Edition 10 users, and that is why, if we want to use a real Unix/Linux based system, then we have to use containers. Virtual machines don't really appeal to me in this case because of the resource consumption they represent.

If you are using a Professional or Enterprise version, you are very likely to be able to use the WSL. This has a great advantage because no container will be needed and to configure Intellij you only need to provide bash as the Terminal command.

SDK-Man is always a favorite of mine, because of how easy it is to install Java versions. The only disadvantage we may point out is that they have a fixed number of different Java versions where we don't control the addition. Using apt we can add a lot more, but it does add quite a bit of complexity to the system.

We could have done all of this without choco and without PowerShell, but that would add other challenges and would probably make this installation much more complex.

I hope that you enjoyed this article as much as I enjoyed writing it.

If you enjoyed it, please share it in the links below.

Take care, stay interested, stay logic, stay safe!


If you happen to be interested into further deepen your knowledge about systems, you might want to have a look at a video I created about saving a system that appears to be broken for good over here:


7. Resources

Top comments (0)