Wednesday 7 September 2016

Configer Sublime Text Editor to compile and run java program


Sublime Text is an editor which has the balance between simplicity and power. Many programmers are using Sublime for their daily programming.

       By default, Sublime can compile a Java source file as long as the Java compiler (javac) can be found via the PATH environment variable. So make sure you update this variable so Sublime can interact with the Java compiler.

To compile a Java source file from within Sublime make sure you have makred Tools>Build System>javaC as selected then press Ctrl + B (or click Tools > Build). If the Java compiler couldn’t be found, Sublime will display an error message like this:

[Error 2] The system cannot find the file specified
javac not found


Once you corrected the PATH variable, you may need to restart the editor to take effect. Then press Ctrl + B again, you would see the successful message like this:
finished compile



To Run the compiled java class you may need to do some extra steps.

To run a compiled class using sublime text you will need to create your own Tools>Build System>javaC like build configration e.g. Tools>Build System>myCustomeConfige or you can modify the exitsing configrations as well.

Follow the steps given and you will be able to run java program on linux using Sublime Text Editor

Go To Tools >Build System> New Build System
This will open a new file with name untiteld.sublime-build . This is actully a sublime build config file where we can put our own configation to operate any file using console. We will write some code here which will be called while we will press Ctrl + B (or click Tools > Build). To run java program copy and pest the following code in in this build file and save it with your custome config name like MyConfig.sublime-build.


{
"shell_cmd": "javac -Xlint \"${file}\"",
"file_regex": "^(...*?):([0-9]*):?([0-9]*)",
"working_dir": "${file_path}",
"selector": "source.java",


"variants":
[
{
"name": "Run",
"shell_cmd": "java \"${file_base_name}\""
}
]
}


Save the file on /home/userSk/.config/sublime-text-3/Packages/User/Java , That's it

Now go to Tools>Build System here you will be able to see you own build confige with name MyConfig. Now mark Tools>Build System>MyConfig as selected and press Shift +Ctrl+ B (or click Tools > Build With) . It will show you two commands MyConfig and MyConfig - Run. If you will select MyConfig it will compile your code or if will select MyConfig – Run it will run the class. You can also do Compile and Run in sigle hit of Ctrl + B (or click Tools > Build) by making relevnt configration.


For more information goto the link http://docs.sublimetext.info/en/latest/reference/build_systems/configuration.html

Using Mongo DB Aggregation With Spring Mongo Template

Using aggregation in spring mongo template is quit easy and interesting .Before going foreword let's see a example of aggregation in mongo db console client.

If have collection "messages" in mongo db where i have fields like mailboxId and unreadMessageCount and some other fields like isVisible .

Then the aggregate query for Monog DB console will be like

Console Query:

db.messages.aggregate(   [
    {
        "$match":{"mailboxId":"1","isVisible":true}
            
    },
     {
       "$group":
         {
           "_id": "$mailBoxId",
           "unReadMessagesCount": { "$sum":"$unReadMessagesCount" }
         }
     }
   ]);
     
Console Result :

/* 1 */
{
    "result" : [ 
        {
            "_id" : "1",
            "unReadMessagesCount" : 12
        }
    ],
    "ok" : 1.0000000000000000
}

Aggrigate query in mongo db give list of result objects ._id is mendatory in $group

Aggrigate in Spring Mongo Template

To get the same result using mongo template we need to create same query object in our java code and we also need a java class to parsh the result and to fetch values.

Lets say we have java class AggResultObj.java to fetch result object

public class AggResultObj{
String _id;
int unReadMessagesCount;

public String get_id() {
return _id;
}
public void set_id(String _id) {
this._id = _id;
}
public int getUnReadMessagesCount() {
return unReadMessagesCount;
}
public void setUnReadMessagesCount(int unReadMessagesCount) {
this.unReadMessagesCount = unReadMessagesCount;
}
}


Then use the following code to populate aggrgated result in form of this object. Use the following code sanippet.


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.query.Criteria;

public class DummyTest {

@Autowired
MongoTemplate mongoTemplate;
public void test() {
Aggregation agg = Aggregation.newAggregation(
Aggregation.match( Criteria.where("mailBoxId").is("1").and("isVisible").is(false)), Aggregation.group("$mailBoxId").sum("$unReadMessagesCount").as("unReadMessagesCount")
);
System.out.println("Query ==>>["+agg.toString()+"]");
AggregationResults<AggResultObj> data = mongoTemplate.aggregate(agg, "messages", AggResultObj.class);
System.out.println("UnReadMesasgeCode :"+data.getUniqueMappedResult().getUnReadMessagesCount());
}

}


Configer Sublime Text Editor to compile and run java program

Sublime Text is an editor which has the balance between simplicity and power. Many programmers are using Sublime for their daily program...