Thursday, 24 July 2014

Tweet using Java (In 7 easy steps) [Tags: #Twitter4J,#Java,#SimpleJavaPro,#TweetUsingJava]

Welcome again.
          We all know that Java is very rich programming language,here is the another example of that.
  In this program you will know how to Tweet on Twitter using java.(If you are new to Java this tutorial is Sufficient for you).


Requirements:

1. JDK 1.6 or 1.7.

   If not installed, download and install it.
Download JDK here.

2. Eclipse IDE.

   If not installed, download and install it.
Download Eclipse here.

3. Basic knowledge of Java.

     If do not have,learn basics.
Learn Basic Java here.

(Note: Before starting this tutorial I am assuming that all requirements are fulfilled by you.)

Let's start it.



Step 1: You required 4 things to post on Twitter™ using Java.

  1. API Key-  (Twitter API keys code)
  2. API Secret-  (Twitter Consumer secret code)
  3. Access Token-  (Twitter Access Token code)
  4. Access Token Secret-  (Twitter Token secret code)
You will get them here.Click to get them.

  • Login and Click on "Create New App"


  • Add details as follows (leave callback URL blank)

  • Click on "Create your Twitter application".
  • In "Application Settings" Click on "modify app permissions". 


  • Select "Read, Write and Access direct messages" and Click on "Update Settings".


  • Click on "manage API keys".                                                                                                       





  • Copy/Save  'API key' and 'API secret' because they are from 4 required codes.





    • Scroll down and Click on 'Create my access token'.


    • Copy/Save 'Access token' and 'Access token secret'.                                                                 

    • Smile, half work is done.....:-), let's code now.....
    • (Note: Before going to second step I am assuming that you have completed step 1 and you have four things....[i.e...API Key,API Secret,Access Token,Access Token Secret])...


    Step 2: Open Eclipse IDE click on "File>New>Java Project" and name your project.


    • Click on finish.                                                                                                                                 


    Step 3: Download Twitter jar file here.

    Download Twitter Jar.


    Step 4: Buildpath jar file to your project "Right click on project>Build Path>Configure Build Path>Add External Jar".


    • Select jar file wherever it is.

    Step 5: Create class,name it  'TwitterMain' (or whatever).




    Step 6: Now Code "TwitterMain.java".


    import twitter4j.Twitter;
    import twitter4j.TwitterException;
    import twitter4j.TwitterFactory;
    import twitter4j.auth.AccessToken;

    public class TwitterMain {

    static String APIKey = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
    static String APISecret = "XXXXXXXXXXXXXXXXXXXXXXXXXXXX";
    static String accessTokenStr = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
    static String accessTokenSecretStr = "XxxxxxxxxxxxxxxxxXXXXXXXXXXX";

    public static void main(String[] args) {

    try {
    Twitter twitter = new TwitterFactory().getInstance();

    twitter.setOAuthConsumer(APIKey, APISecret);
    AccessToken accessToken = new AccessToken(accessTokenStr,
    accessTokenSecretStr);

    twitter.setOAuthAccessToken(accessToken);

    twitter.updateStatus("#SimpleJavaPro #TweetUsingJava #java #Twitter4J http://simplejavapro.blogspot.in/2014/07/");

    System.out.println("Successfully updated the status in Twitter.");

    } catch (TwitterException e) {
    e.printStackTrace();
    }
    }
    }


    //(Note: Don't forget to add API keys,secrets,accessToken,TokenSecret at XXXXXXXXXXXXXX).


    Step 7: Run it...

    Congrats.....you've done....Tweet using Java™.




    Download ArgsEx1.java
    Download "TweetUsingJava"
                                                             

    Wednesday, 16 July 2014

    Java Command-Line Arguments Simple Example.

    Command-Line Arguments:

    • Many times we use command prompt (i.e. "cmd" in Windows) to compile and run our java program.
    • If we want to get any input from user at runtime / specifying configuration details when the application is launched we use "Command-Line Arguments".
    • Now,let's see an Example for that,


       —————————————————ArgsEx1.java—————————————————

    public class ArgsEx1{                                          ——Line 1——
        public static void main (String[] args) {                  ——Line 2——               
            for(String input:args){                                ——Line 3——
    
                    System.out.println(input);                     ——Line 4——
    
                }
          }
     }
    



    Output:


    ArgsEx1Output


    Explanation:

    • In this example Simple Java Pro are called command-line arguments as shown in image.
    • When you run the program with values as input (Here: Simple Java Pro),It will print word line by line.Because the space character separates command-line arguments.
    • You can use any number of command-line arguments.
    • The Line 3 will first store value:"Simple" in variable "input" and prints it,then "Java" and prints it,then "Pro" and prints it.

    Note:

    • If you want to use a string contains space in command line arguments then use double quotes for that,
              Example:  "Simple Java Pro"

              —It will tell JRE to see whole String as a single command,try this.

    Download ArgsEx1.java
    Download ArgsEx1.java

    Sunday, 6 July 2014

    Simple Java Servlet Hello World example. (In 10 easy steps)

    Welcome to my new post,

                Here I have done hello world example using java servlet. Here are some requirements before doing this example.


    Requirements:

    1. JDK 1.6 or 1.7.

       If not installed, download and install it.
    Download JDK here.

    2. Eclipse IDE.

       If not installed, download and install it.
    Download Eclipse here.

    3. Apache Tomcat 7(recommended) or Tomcat 6.

        If not installed, download and install it.
    Download Tomcat here.

    4. Basic knowledge of Java.

         If do not have,learn basics.
    Learn Basic Java here.

    (Note: Before starting this tutorial I am assuming that all requirements are fulfilled by you.)


    Step 1: Open Eclipse IDE click on File>New>Dynamic Web Project.
    (if not found dynamic web project in 'New' click on 'other'.)




    Step 2: Now, Name you project 'SevletHelloWorld'.




    Step 3: Click to Next> two times, you will see below window.
    • Tick on Generate web.xml deployment descriptor, it will be explained further.



    • Now,click in 'Finish'.
    • Now Your project Structure is ready,lets start development.

    Step 4: Copy following .jar file to lib folder located in "WebContent>WEB-INF>lib".

    • Download only Servletapi-2.4.jar.
    • Download .jar file, Download here.
    • Download and put it into lib folder.


    Step 5: Right Click on project click to "New>Class".
    • Set Class Name "HelloWorldServlet".
    • Click to finish without doing anything else.

    Step 6: Now Write following code in HelloWorldServlet.


    import java.io.IOException;
    import java.io.PrintWriter;
    
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    
    public class HelloWorldServlet extends HttpServlet{
    
           public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException{
          response.setContentType("text/html");
          PrintWriter out=response.getWriter();
      
                        out.println("Hello world.");
     }
    }
    

    //*It will look something like this.
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    Step 7: Now change in web.xml file located in "WebContent>WEB-INF".
    
    
    
    
    
    

    
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns="http://java.sun.com/xml/ns/javaee"
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
     id="WebApp_ID" version="3.0">
     <display-name>ServletHelloWorld</display-name>
    
     <servlet>
      <servlet-name>ServletHelloWorld</servlet-name>
      <servlet-class>HelloWorldServlet</servlet-class>
     </servlet>
    
     <servlet-mapping>
      <servlet-name>ServletHelloWorld</servlet-name>
      <url-pattern>/index.html</url-pattern>
     </servlet-mapping>
     
     <welcome-file-list>
      <welcome-file>index.html</welcome-file>
     </welcome-file-list>
     
    </web-app>

    //*It will look something like this.


    Step 8: You project is ready now it's time to run it.
    (Note: I am assuming that you have already installed apache tomcat).
    
    
    • Right Click on project "Run as> Run on Server >".
    
    
    
    
    Step 9: Select apache tomcat then select your installed apache tomcat version.
    
    
    
    
    • I have used tomcat 7.
    • Click on Finish
    • If everything goes correct you will see following output.
    Step 10: Congratulations it's done.
    
    
    For any query ask in comment section.
    I hope it helps you,thanks.
    Amazing is helping someone,Zaid Khan.
    
    

    To Download complete project,Click here.