Understanding Java ClassPath

 Java class path


```

java -cp <path_to_jar>:<path_to_current_class> Main.class -> for linux

java -cp <path_to_jar>:<path_to_current_class> Main.class -> for windows

```

java -cp ..:. Main


.. indicates the previous directory

; | : separator for linux or windows

. indicates the current working directory 


loading classes from jar

to create jar


jar cvf <jar_file_name.jar> class1.class class2.class

java -cp ..:. /path/to/jar:. Main


compiling Java file where dependent classes not in the same directory,


javac -cp <path-to-deps.java>:<path_to-current_class> <className>


via jar

javac -cp <path_to_jar>:<path_to_current_class> <class_name>

How to see logs with out Esc and other gibberish stuff in logs ?

 So when you are trying to read logs, sometimes they look like this, using vim is okay at-least some readability, but less will show awful results. 

ESC[34m2023-04-15 04:33:57ESC[0m ESC[33m  TRACEESC[0m [ESC[36mApplicationESC[0m] ESC[37mpid: 562425ESC[0m

ESC[34m2023-04-15 04:33:57ESC[0m ESC[35m  DEBUGESC[0m [ESC[36mApplicationESC[0m] ESC[37minteractive: falseESC[0m

ESC[34m2023-04-15 04:33:57ESC[0m ESC[35m  DEBUGESC[0m [ESC[36mFileResolverESC[0m] ESC[37mSearching: /server/files ESC[0m

if you want to read logs clearly, then you can still use less command with -R flag.

less -R <log_file_name>


Hope it helps.

Thanks.

Raja

VIM: Binding a shortcut

 All this time, I do not know how to create my own VIM Bindings. And yes, trying yourself is the only way you learn anything.

I want to explain what I have learnt on creating a vim shortcut, so you might get a good headstart.

The command I am going to explain is as below


map <C-b> :w\|!ruby spec/example_spec.rb <cr>

so

  • map: is like you are mapping <custome_key> <series of actions>
  • <C-b>: this stands for CTRL+b 

If you want to use Shift, Alt, Cmd(Mac), yes VIM have options for all of that.

```

S-...>		shift-key			*shift* *<S-*

<C-...>		control-key			*control* *ctrl* *<C-*

<M-...>		alt-key or meta-key		*meta* *alt* *<M-*

<A-...>		same as <M-...>			*<A-*

<D-...>		command-key (Macintosh only)	*<D-*
```
Vim documentation: intro (sourceforge.net)
  • Then :w\|!ruby spec/example_spec.rb<cr>
    • :w is save
    • \|: is like you are joining this command with with another action.
    • !ruby spec/example_spec.rb
      • ! is for invoking shell commands, for example give :!uptime, you will see what I am saying.
      • ruby spec/example_spec.rb, you know its ruby command executing the file.
      • <cr>: This carriage return equal to <Enter> key, with out this, your command will be loaded but to execute you have to press enter again. so adding <cr> at the end, will help executing your command.

Based on above understanding, lets say you have a python file like service.py, you want to execute it with a VIM binding, you can use below command
map <C-r> :w\|!python service.py <cr>
so on CTRL+r, I will be running my python program.

Hope it helps.
Thanks
Raja

Javascript: call vs bind vs apply

 Hello Everyone, 

I came across a snippet where code have .call() method.

Thought its been a while since I went through their understanding, via this article I am explaining with examples what call, bind and apply does in javascript.

Please read comments for understanding.

Hope it helps.

var obj = {
name: "Raja",
sayHello: function(args) {
console.log(`Hello ${this.name}, How are you doing `);
},
};

// call : Changing the context::this scope to the argument we are passing.
// normal

obj.sayHello() // Hello Raja, How are you doing ?
obj.sayHello.call({name : "Rajasekhar"}); // Hello Rajasekhar, How are you doing ?

var obj = {
name: "Raja",
sayHello: function(args) {
console.log(`Hello ${this.name}, How are you doing ? ${args}`);
},
};
// apply: same as call, we can also pass arguments as 2nd argument
obj.sayHello.apply({ name: "Rajasekhar" }, ["Testing"]); // Hello Rajasekhar, How are you doing ? Testing


// bind: Change function definition to a different context
// you can change the definition once

var obj = {
name: "Raja",
sayHello: function(args) {
console.log(`Hello ${this.name}, How are you doing ? ${args}`);
},
};

var contxt = {"name" : "ApplyContext:)"};
var newHello = obj.sayHello.bind(contxt);
newHello("Good Day!!!"); // Hello ApplyContext:), How are you doing ? Good Day!!!

GoLang: Quick gist on Difference between Marshal, Unmarshal, Encoder and Decoder

Summary: Marshal, Unmarshal and Encode, Decode

Summary: Marshal, Unmarshal and Encode, Decode

  • Marshal: Convert struct/map interface data into JSON.
  • Unmarshal: Convert JSON data into struct/map

If reading from a file involved, then you read it in []byte format. To read from a file

  • using os.Open open the file.

  • then read all the data using io.ReadAll method.

  • Encoder and Decoder kind of do reading/writing internally. More like a simplified version of Marshal and Unmarshal.

  • Encoder converts the struct/map into JSON and automatically writes into io.Writer

  • Decoder reads from io.Writer and convert the JSON data into struct/map interface.

If there is a byte slice to work with, use Marshal and Unmarshal. If there is an io.Writer or an io.Reader, use Encode and Decode.

Ref: https://blog.devgenius.io/to-unmarshal-or-to-decode-json-processing-in-go-explained-e92fab5b648f

GoLang: How to render a web template using Gin framework and render data from map ?

 I have struggled lot of time, to break this into working modal. So below code, is working example where you can have your data in a slice of URLs and render that data using Go Templates. 

I am giving a skeleton HTML template, not adding any thing extra. 

I believe the code itself is self explanatory, if any questions, please dont hesitate to ask.


main.go

package main

import (
"log"
"net/http"

"github.com/gin-gonic/gin"
)

type URL struct {
Name string
Url string
}

var URLs = []URL{
{Name: "google", Url: "google.com"},
{Name: "yahoo", Url: "yahoo.com"},
{Name: "gmail", Url: "gmail.com"},
}

func main() {

router := gin.Default()
router.LoadHTMLGlob("templates/*")
router.GET("/", bookmarks)
router.GET("/hello", func(c *gin.Context) {
c.HTML(http.StatusOK, "index.tmpl", gin.H{
"title": "Main website",
"Items": URLs,
})
})

log.Fatal(router.Run(":8000"))
}

func bookmarks(c *gin.Context) {
c.HTML(http.StatusOK, "index.tmpl", gin.H{
"title": "Bookmarks",
"URLs": URLs,
})
}

Go Templates will be stored inside templates directory, so create a templates directory where you have your main.go file and have that file as below 

index.tmpl


<html>
<h1>
{{ .title }}
</h1>
<body>
{{ range .URLs}}
{{.Name}}
{{.Url}}
{{end}}
</body>
</html>

Hope it helps.

Thank you.