Friday, February 7, 2020

run script at startup of ubuntu

Run script at startup (when you login). Following example add a small memory check script at the startup


  1.  cd  /usr/share/applications
  2. sudo gedit  memcheck.desktop
  3. [Desktop Entry]
    Name=memcheck
    Comment=memcheck  Tool
    Exec=/home/***/projects/mem_alert/memcheck.sh
    Terminal=true
    Type=Application
    Icon=someicon copied from existing .desktop files in same folder
  4. Open tweaks
  5. install genome tweak tool
  6. Click "startup applications"
  7. Click add, then select "memcheck" application from the list. If it do not appear then you have done some thing wrong.
  8. Now logout and login. The mem check script should run after user login


My memcheck.sh looks like:


#!/bin/bash
#Minimum available memory limit, MB
THRESHOLD=2000
#Check time interval, sec
INTERVAL=30
while :
do
    free=$(free -m|awk '/^Mem:/{print $4}')
    buffers=$(free -m|awk '/^Mem:/{print $6}')
    cached=$(free -m|awk '/^Mem:/{print $6}')
    available=$(free -m|awk '/^Mem:/{print $7}')
    message="Free $free""MB"", buffers $buffers""MB"", cached $cached""MB"", available $available""MB"""
    if [ $available -lt $THRESHOLD ]
        then
        notify-send "Memory is running out!" "$message"
    fi
    echo $message
    sleep $INTERVAL
done

Monday, May 20, 2019

Install openjdk 11 at ubuntu


  •  Download jdk11 from https://download.java.net/openjdk/jdk11/ri/openjdk-11+28_linux-x64_bin.tar.gz
  •  Extract to /usr/lib/jvm 
sudo tar xvf openjdk-11+28_linux-x64_bin.tar.gz --directory /usr/lib/jvm/
  • sudo update-alternatives --config java
  Selection    Path                                     Priority   Status
------------------------------------------------------------
  0            /usr/lib/jvm/jdk-11/bin/java              3         auto mode
  1            /usr/lib/jvm/java-7-oracle/jre/bin/java   1         manual mode
* 2            /usr/lib/jvm/java-8-oracle/jre/bin/java   2         manual mode

  • Install java11 as 3 option
sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/jdk-11/bin/java 3
sudo update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/jdk-11/bin/javac 3
  • Set java11 as current java version 
sudo update-alternatives --config java and select 3
sudo update-alternatives --config javac and select 3

Wednesday, November 2, 2016

Kernel panic after ubuntu update

I recently did ubuntu update and then after reboot i got following screen related to kernel panic. The problem for me was that my /boot ran out of space.



Solution:

Restart your machine and go to grub loader
Select "Advanced options for Ubuntu"

Then you will be listed with list of kernels, select one of them and press enter [at least one of them should allow to you boot and login  into your ubuntu machine]

Then go to terminal and run:
 aptitude search ~ilinux-image
this should list all the kernel images u have in /boot

run
uname -a

to get ur current running kernel

run
sudo apt-get <image name>
for example: sudo apt-get autoremove linux-image-4.4.0-45-generic
to remove the image that you dont need

Saturday, June 11, 2016

Ambigious mapping

Scenario:  

Need to add a spring mvc project as dependency (scope test) to other spring mvc project.

Problem: 

Ambiguous mapping. Cannot map 'xyzController' method 

Reason: 

Both projects had web controller with request mapping annotation only at method level. As the rest of it when spring tries to map / , we have ambibious maping scenario where both the controllers quality for the mapping and this result in the exception.

Solution: 

Add Requestmapping annotation at the both controller (or one of them) so that spring can map urls distinctly


Monday, April 27, 2015

Generic enum converter

  public static Enum<?> toEnum(final Class<? extends Enum> classType, final Enum<?> enumObj) {  
     if (enumObj == null) {  
       return null;  
     } else {  
       return enumObj.valueOf(classType, enumObj + "");  
     }  
   }  

Usage:

  Test1 returnedEnum = (Test1) toEnum(Test1.class, Test2.HELLO_ENUM);  

Tuesday, February 3, 2015

Documenting Spring annotated rest interface via reflection  

1:   for (Method method : YourClass.class.getMethods()) {  
2:        // url  
3:        if (method.isAnnotationPresent(RequestMapping.class)) {  
4:          RequestMapping parms = method.getAnnotation(RequestMapping.class);  
5:          String[] val = parms.value();  
6:          RequestMethod[] methodType = parms.method();  
7:          for (int i = 0; i < val.length; i++) {  
8:            System.out.println(val[i] + "     METHOD TYPE:" + methodType[i]);  
9:          }  
10:          //parameter  
11:          Class<?>[] paramsTypes = method.getParameterTypes();  
12:          Annotation[][] annot = method.getParameterAnnotations();  
13:          for (int i = 0; i < annot.length; i++) {  
14:            if (annot[i].length > 0 && annot[i][0] instanceof RequestParam) {  
15:              RequestParam new_name = (RequestParam) annot[i][0];  
16:              System.out.println("Request: " + new_name.value() + " = " + paramsTypes[i]);  
17:            }  
18:          }  
19:          //return type  
20:          if (method.getReturnType().getCanonicalName().equals(List.class.getCanonicalName())) {  
21:            Type listType = method.getGenericReturnType();  
22:            if (listType instanceof ParameterizedType) {  
23:              Type elementType = ((ParameterizedType) listType).getActualTypeArguments()[0];  
24:              System.out.println("Response: [" + elementType + "]");  
25:            }  
26:          } else {  
27:            System.out.println("Response: " + method.getReturnType().getCanonicalName());  
28:          }  
29:        }  

Tuesday, September 9, 2014

Problem in getting post parameter in Spring MVC via REST request

@RequestMapping(value = "/api/login", method = RequestMethod.POST)
    public WelcomeMessage getHelloWorld(@RequestParam(value = "username") final String username, @RequestParam(value = "password") final String password) {
            // do some thing
    }


I was having the problem to get post parameter via REST request.  Adding the header "Content-Type:application/x-www-form-urlencoded" fixed the issue