ajduke`s blog
technical notes on software development
Install, setup and start MongoDB on Windows
Posted by on April 10, 2013
This post will provide the full path from downloading required binary archive/package for particular Windows version to starting up MongoDB in various ways
Following are the high level steps
- Download the MongoDB binary archive
- Extract MongoDB archive
- Setup up configuration parameters and start/stop MongoDB
- using command line
- using windows services
Download the MongoDB binary archive
For Windows platform, MongoDB distributes zip archive. Go to following downloads page from browser http://www.mongodb.org/downloads
Depends on system architecture, it comes in two distribution as
- 32-bit
- 64-bit
Again, MongoDB distribution for Windows 64-bit ships with two flavours
- one for Windows server 2008 and Windows 7, Server 2012 (download link “*2008R2+” )
- other for rest of 64-bit Windows OS.
This distinction for x64 is made based on newer OS features which helps in enhanced performance of MongoDB.
Choose the production releases for downloading
After you download, you will get zip archive named like mongodb-<platform>-<architecture>-<version>.zip
Extract MongoDB archive
Once we have MongoDB archive, go ahead and extract archive using any zip extract program.
After extracting, you will get the directories inside archive as follows
here , bin directory contains the binaries in form of executables , such as mongod.exe, mongo.exe, monogexport.exe etc.
Setup up configuration parameters and start/stop MongoDB
For starting and stopping the mongodb server, we need only the bin\mongod.exe, which is the daemon process executable for MongoDB. In short, it is the executable which drives up the MongoDB in general
For starting up, we need to provide the parameters for executable, which i call it here as config parameters or params
We can setup the config parameters using the two ways
- Using command line options or
- Using config file
Using command line options
With use of these command line options, we configure mongo daemon process. Basically, there lots of options we can specify but i will give only those which required for this tutorial.
Following are some of them
–dbpath <path> : the existent directory path, which is required to store data files. this is most important option we need to specify, Note that, the directory path you are providing should exists otherwise process won’t start. If this path contains spaces then put all its path in double qoutes. e.g. –dbpath “c:\Program Files”
–logpath <log-file-path>: the existent file path, used by mongo daemon process,for flush out loggs instead of in standard console. If this path contains spaces then put all its path in double qoutes
–port <port> : port no. where mongod process listen for connection from client, it defaults to 27017 if not specified
Note : While using the command prompt on some Windows OS like windows 7 or Windows server 2008, Run it with administrator privileges as shown as follows
Use the following commands to start the server process
Change to bin directory
> I:\>cd Servers\mongodb\bin
now type following command to start the mongod process
> mongod --dbpath I:\Servers\data --port 27017
While starting, Windows firewall may block the process as shown as follows
Click “Allow access“ to proceed.
After successful execution of command , it will show logging info in standard console itself as shown follows
> I:\Servers\mongodb\bin>mongod --dbpath I:\Servers\data --port 27017
Tue Apr 09 22:49:13 [initandlisten] MongoDB starting : pid=4380 port=27017 dbpath=I:\Servers\data 64-bit host=Myi-PC
Tue Apr 09 22:49:13 [initandlisten] db version v2.2.1, pdfile version 4.5
Tue Apr 09 22:49:13 [initandlisten] git version: d6764bf8dfe0685521b8bc7b98fd1fab8cfeb5ae
Tue Apr 09 22:49:13 [initandlisten] build info: windows sys.getwindowsversion(major=6, minor=1, build=7601, platform=2, service_pack='Service Pack 1')
BOOST_LIB_VERSION=1_49
Tue Apr 09 22:49:13 [initandlisten] options: { dbpath: "I:\Servers\data", port: 27017 }
Tue Apr 09 22:49:13 [initandlisten] journal dir=I:/Servers/data/journal
Tue Apr 09 22:49:13 [initandlisten] recover : no journal files present, no recovery needed
Tue Apr 09 22:49:13 [initandlisten] waiting for connections on port 27017
Tue Apr 09 22:49:13 [websvr] admin web console waiting for connections on port 28017</em>
If you specify the logpath option, then logging will direct to that log file instead of showing up on standard console
> mongod --dbpath I:\Servers\data --port 27017 --logpath I:\Servers\logs\mongod.log all output going to: I:\Servers\logs\mongod.log
and prompt will wait there and you can find all the logs at specified log file location
You can stop this process with use of keys Ctrl+C or Ctrl +D from keyboard
Using the config file
Instead of specifying command line option, we can specify same with use of file, which i call it here as config file
Config file is just normal file, containing the parameters in the key=value form and each is on the every line of file
In this, we basically provide path to file (which contains the configurations) as command line option as “-f” or “–config”
Following is the snippet for the config file
#This is an example config file for MongoDB #basic dbpath = I:\Servers\mongodb\data port = 27017 logpath = I:\Servers\mongodb\logs\mongo.log
You can save this file with any extension, but specify full path with extension, while stating process as shown in following commands.
From command prompt, you will use either of following
> mongod -f I:\Servers\mongodb\config\mongodb.conf
or
> mongod --config I:\Servers\mongodb\config\mongodb.conf
Start/Stop MongoDB using the Windows services
Support for installing mongod server as service comes out of the box.
Mongodb daemon executable provides amazing support for the installation of services using few command line parameters without using additional components for this purpose Just we need set the few command line params and we are way to go and they are as follows
Following are required parameters
–install : command line switch to install the service
–remove : command line switch to remove the service
–serviceName <name> : the name for mongod windows service and must adhere to naming services in windows like only accepting the alphanumeric chars with no spaces
–serviceDisplayName <display-name> : Display name for service that is shown in services console, put this in double quotes if contains the spaces
–serviceDescription <description> : Small description about service, put this in double quotes if contains the spaces
While installing as service we must provide log file path as counterpart to starting it from command line, because while starting service we don’t as standard console
I will be using the config file for some configurations
> mongod -f "I:\Servers\mongodb\config\mongodb.conf" --install --serviceName mdb27017 --serviceDisplayName "MongoDB Server Instance 27017" --serviceDescription "MongoDB Server Instance running on 27017"
In log path specified, you can check for the whether windows services started or not
Above will install the mongodb as Windows service, check Services console using services.msc
Now we can start or stop MongoDB using Windows services console as shown above.
You can remove the service using following
> mongod -f "I:\Servers\mongodb\config\mongodb.conf" --remove--serviceName mdb27017 --serviceDisplayName "MongoDB Server Instance 27017" --serviceDescription "MongoDB Server Instance running on 27017"
Setting up new Java compiler and runtime in Eclipse IDE
Posted by on April 28, 2013
This article will present migrating Eclipse development environment from one version of Java to another in general but Java 6 to java 7 in particular.
Also, it will show you way to configure the Java compiler and runtime in Eclipse IDE for Java / Java EE.
Setup compiler and runtime at global level
By default in the eclipse, all projects uses the same compiler and runtime setting which is configured at global level. The term global level, we will use it here for common setting for all Java projects.
Set up compiler
While installing/setting up the Eclipse IDE on machine for first time, you may notice that you don’t require the JDK to be configured for Eclipse IDE. But It needs the JRE (it gets from default JRE for machine) and still Eclipse will compile and run our all the Java projects
This is because Eclipse IDE has in built incremental compiler which is packed with JDT(Java Development Tools). So we don’t need to give path to javac and so forth.
The Eclipse JDT tooling has way to change the compiler using the which, Eclipse call it as Compiler Compliance level. It indicates which JDK level, source code should conform to.
By default, initially JDT will set the compiler level according to JRE you configured initially while starting it for first time.
Now, do following to configure compiler for Java projects
Under Menu, Go to Windows > Preferences >> Java >> Compiler
or
Press shortcut key – Ctrl + 3 > type “compiler” >> choose “Compiler-Java “ under Preferences category as shown below
after this you will have
Now, change the compiler level of your choice as shown below, in this case we will change to 1.7 from Compiler compliance level dropdown
Setup runtime
For running the eclipse itself it requires the Java Runtime and it finds using path variable or registries or value of “-vm” param in eclipse.ini file and after that it automatically configures this JRE by default for all Java project
Under Menu, Go to Windows > Preferences > Java > Installed JRE
or
Press shortcut key – Ctrl + 3 > Type “Installed” > Select Installed JRE under Preferences category as shown below
after this you will see following
You can add the new JRE at using Add button which tell you to browse for JRE installation folder as shown in following figures
After this you can select the new JRE, in our case jre7.
Note that, you can select only one JRE from configured ones from Installed JRE.
Setup new compiler and runtime for existing project
By doing above configuration make sure the new compiler and runtime for new Java projects.
For existing projects, you need to do changes in the project specific properties after the above configuration
Following are the steps required
-
Change the library from build path
-
make sure check for using generic/global level compiler compliance level
-
Build the project
Change the library from build path
You need to change the JRE system library from build path for that project, which is shown as follows
Go to projects Build Path by right click on project > Build Path > Go to Configure Build Path…
Select JRE library and hit the Edit button from right side buttons and it will open up the dialog box for the changing the JRE library
Now, select the either Alternate JRE radio button and change to jre7 from dropdown
or you can select third radio button, Workspace default JRE, which will use the jre7 for our case
Click OK on this pane and again on build path pane as shown in following figure
Compiler compliance level
For a particular project, if you are configured the Project specific for Java compiler compliance level, make sure it will use the global configuration or you can make it to use the level as 1.7. You can check it for as shown as follows
Go to Project , right click go to Properties menu > Java Compiler as shown as follows
Above shows the project properties pane, on right select the Java compiler and then if you are ticked “Enable project specific setting “, then untick that, otherwise ignore this step
Build the project
After all above steps, build the project in Eclipse.which will use the new Compiler level and JRE.
If you are doing Build automatically, then project is already built after you changed the project.
Now, you will have the all projects in your Eclipse IDE using the newly configured Java compiler and runtime.
Java Compilation Unit
Posted by on September 23, 2012
It is basically referred to a Java source code file, which forms the input for the Java compiler which is javac.
This term is less commonly used among developers and it is technical jargon for Java source code file.
This Java compilation unit has structure which is as follows -
[ package declaration ] [ import declararion/s or static import declaration/s] [ top-level type declaration/s; ]
Some points about the compilation unit, these are somewhat based on compiler-
- may or may not contain the above components
- if anyone of them present, then must go in selected slots and follow order as shown above, such as, package declaration should be before any of type declaration
- can contain the one or more type declaration must go in the order as shown above, type in this case can be class, interface, enum or annotation
- if it contains public access level type then it must be only one and can have no. of non-public/default level types, In other words, it can contain ’1′ public type and any ‘ n’ non-public types
- name of compilation unit can be - name of the public type, if present or any type name inside it or anything you like provided no public type are present inside it
- non-javadoc comments can go anywhere, does not necessary follow any order as shown above structure.
Following code listing contains sample Java compilation
This should be named MyClass.java
// compilation unit package declaration
package compilation.unit.one;
// import declaration goes here
import java.util.ArrayList;
import java.util.List;
import static java.lang.Math.random;
import static java.lang.System.out;
// rest of the compilation unit has type declarations
// a class declaration which is public and compilation unit carries this name
public class MyClass {
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
list.add("One");
// static import for out
out.println(list);
// static import for random mehod
double random = random();
out.println(random);
}
}
// interface declaration
interface Actions {
void doStuff();
}
// enums declaration
enum State {
ONE, TWO
}
// annotations declaration
@interface Annotate {
}
// end of compilation unit
Java : Uses of this and super
Posted by on August 19, 2012
In this post, I will list out possible uses of these two operators. Before that let me give brief details
- These are keywords having special semantic/meaning in language.
- Most uses are accessing the members or invoking the constructor.
- Uses of are valid within- Constructor, Instance initializer block, Instance initializer expression. Use in constructs than these are invalid and compiler will throw error accordingly.
Uses of this
It is operator which represents the value which is reference to object to which instance method is invoked (in case of method or block scope) or object in the construction (constructor scope)
In simple words, when using this operator you have access to invoking object, to which you can access any instance member on that.
Access the members
You may modify access to members using this operator to explicitly saying that accessing the member of that class. Without modifying it will access only member which belongs to same class
Basic syntax is this.<member>
Following snippet shows usage
Following program shows the this reference value and Shape reference variable, to which method is invoked, are same
Above code makes clears that this is the reference to invoking object
Access member variable in case parameter name is same as field name
There may be the case that you are having same name to parameter as that of field (mostly in constructor parameter) for readability purpose. Lets look at following code snippet
This will not result into assignment to field because instance field is hidden by local variable as it is in local block scope.
To override the local variable scope, we should make use this operator as shown following code
Invoke the another constructor
It may be case that you want to create new constructor which is doing same as that of another constructor but with something different and you don’t want to duplicate code the constructor code
We can have this , using syntax : this ( <param> )
Following code shows usage
Remember, the invocation must be very first statement in the constructor. If it is not then such attempt is invalid.
Use of super
This operator comes into use when class has inheritance and want to access the member of the super class in subclass which are accessible.
Access the hidden member variable
When in a sub class, you are having the variable with same name as that of in super class then super class variable is hidden in the subclass. Therefore, the any access to that variable in subclass will result in to one in the subclass.
So, to access the hidden variable from super class we have to use super as shown in following code snippet.
Access the overridden method from super class
This same as accessing the variable as above
lets look at following code snippet
Invoking the super class constructor
Like invoking another constructor of same class as in this(), you can invoke super class constructor. Using syntax , super(<param>)
Lets look at following code snippet
When you write any constructor, having statements except for this(), then compiler always puts the call to super class default constructor which is super().
Once last point, When using super or this for constructor invocation one of them must be used.
Initializing final member variables in Java
Posted by on May 13, 2012
When you modify any variable declaration with final, you tell to compiler that once variable is initialized, its value cannot be changed.In other words, any further assignment of value is invalid attempt.
While declaring variable as final , compiler also does some bytecode optimization as you might have guessed they going to have only one value while complete execution of program.
In this post i am going to show initialization of the final member variables. As they declared as the top level variable, so you have options of initializing in various constructs .
Initializing final instance variable
You can initialize the final instance variable in only in the one of following constructs.
- Initializer expression
Its value is initialized while declaration, by evaluating its expression on right hand side as shown in following code snippet - Instance initializer block
You can assign a variable a value in its instance initializer block as shown in following code snippet - Constructor block
Finally in the constructor block as shown in following code snippet
Initializing final static variable
When declare static member variable as final then in following constructs, you can initialize
- Initializer expression
While declaring variable, you can initialize value evaluated in initializer expression as shown following code snippet - Static initializer block
The variable can be initialized in static initializer block. Following code snippet shows initialization.While initializing this final variable make sure that you are initializing only in one of the constructs, otherwise compiler throw an error.
