Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: create a test class (main method) to start(run) the class in Java

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default create a test class (main method) to start(run) the class in Java

    Maybe I do not understand something.
    So I've created the class in Java, which does some stuff.I need to create a main method or mainClass, that will start(run) my class. (I do not mean JUnit).
    Question: how to do it?
    is there some sort of methodology?
    described in the books somewhere?

    Help me please


  2. #2
    Member
    Join Date
    Jul 2012
    Posts
    90
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: create a test class (main method) to start(run) the class in Java

    You should create another class that consists of the main class.

    package myproject;
     
    public class Helloworld {
     
    public static void main(String[] args) {
                System.out.println("Hello world");
        }
    }

    Was that helpfull?I mean the structure is like this.I use netbeans IDE.There all i do is to right click on the project ,select new JAVA class,write the class and then creating an object of her for instance in main

  3. The Following User Says Thank You to Samaras For This Useful Post:

    curious725 (July 31st, 2012)

  4. #3
    Junior Member
    Join Date
    Jul 2012
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: create a test class (main method) to start(run) the class in Java

    Yes,I agree.but what if it is something complicated?
    like this

    package handler;
     
    import java.io.BufferedInputStream;
    import java.io.DataInputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.util.ArrayList;
     
    public class TextHandler extends Handler {
    	private ArrayList<String> fileTypes = new ArrayList<String>();
    	private String extension;
    	private String data;
     
    	public TextHandler(Handler next) {
    		super(next);
    		fileTypes.add("html");
    	 	fileTypes.add("htm");
    	 	fileTypes.add("css");
    	 	fileTypes.add("js");
    	 	}
     
    	public Response handle(Request1 recvest){
     
    		 String uri=recvest.getParameter("URI");
    		 if (uri != null) {
    		  String extension = uri.substring(uri.lastIndexOf('.')+ 1);
    	     }else{
    	      return super.handle(recvest);
    		 }
     
    		 if (!fileTypes.contains(extension)){
    		  return super.handle(recvest);
    		 }
    		 String webRoot = Configurator.getInstance().getSettingsParameter("web-root");
    	     String filePath=webRoot+uri;
     
    	     File file = new File(filePath);
    	     if(!file.exists()){
    	      super.handle(recvest);
    	     }
    	     try{
               FileInputStream fis = new FileInputStream(file);
    	       DataInputStream din=new DataInputStream(fis);
    	       byte[] bytes;
    	       int length= din.read(bytes);
    	       String data=new String(bytes,0,length);
    	       din.close();
    	       fis.close();
    	      }catch (FileNotFoundException fnfex) {
    	        super.handle(recvest);
    	      } catch(IOException ioex){
    	        super.handle(recvest);
    	      }
     
    	     String[] ParamArray = {"HTTP/","Server","ContentType","Content-Length","Content-Type","Connection"};
     
    	     String totalResponse = ParamArray[0]+"1.1"+"200 OK"+"/n"+
    	        	    		    ParamArray[1]+"YuriyServer/2009-09-09 "+"/n"+
    	        	    		    ParamArray[2]+(int)file.length()+"/n"+
    	        	                ParamArray[3]+"text"+extension+"/n"+
    	        	    		    ParamArray[4]+"close"+"/n";
    	          ResponsBuilder responsBuilder=new ResponsBuilder();
    	          responsBuilder.buildRespons(totalResponse.getBytes());
    	          responsBuilder.setData(data);
     
    		    }
     
     
    	     }

  5. #4
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: create a test class (main method) to start(run) the class in Java

    Quote Originally Posted by curious725 View Post
    ...what if it is something ...like this
    Well, before you began writing that TextHandler class, you had a plan to test it, right? I mean you must have had something in mind.

    So...

    In a brand new file, create a public class with a main() function that does whatever it is that you had in mind.

    Maybe something like:
    //
    // File Z.java or TestTextHandler.java or whatever name you want for this test class
    //
    // May have import statements...
    //
    public class Z  // Or TestTextHandler or whatever name you want for this test class
    {
     
        public static void main(String [] args)
        {
            // May have "local" variables used in main()
     
            // Create a TextHandler object and whatever
            // other objects you are going to access from
            // main()
     
            // Do stuff with the TextHandler object and whatever
            // other things you need to deal with.
     
        } // End of main()
     
        // May have static data members and methods and inner
        // classes, etc., etc., etc. used in this test class
     
    } // End class definition


    Cheers!

    Z
    Last edited by Zaphod_b; July 31st, 2012 at 01:43 PM.

  6. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: create a test class (main method) to start(run) the class in Java

    If you don't understand my answer, don't ignore it, ask a question.

  7. #6
    Junior Member
    Join Date
    Jul 2012
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: create a test class (main method) to start(run) the class in Java

    Your answer is better.Thank you

Similar Threads

  1. Main method/ class problem. I can't run any script!
    By BokBok in forum What's Wrong With My Code?
    Replies: 3
    Last Post: June 28th, 2012, 05:14 PM
  2. [SOLVED] How to make a class be static if it's the main class.
    By javapenguin in forum What's Wrong With My Code?
    Replies: 18
    Last Post: January 10th, 2012, 05:14 AM
  3. Paint program adding classes to main method class
    By Maxfmc in forum What's Wrong With My Code?
    Replies: 0
    Last Post: April 15th, 2011, 07:01 PM
  4. Creating a scaleUp main method in a new class
    By Brainz in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 16th, 2010, 08:58 AM
  5. How to create a new object of another class within a method
    By davie in forum Object Oriented Programming
    Replies: 1
    Last Post: April 16th, 2010, 05:53 PM