Results 1 to 12 of 12
Like Tree1Likes
  • 1 Post By Tolls

Thread: compilation error - pls help

  1. #1
    codejava is offline Member
    Join Date
    Sep 2011
    Posts
    6
    Rep Power
    0

    Default compilation error - pls help

    Hello,
    I 've four java files which i've listed below. I am trying to compile the code in SeleniumTrafficAnalyserExampleTest.java file , which calls a collection which is in a different java file( TrafficAnalyser.java). I am also listing the code in other two files.

    when I compile the SeleniumTrafficAnalyserExampleTest.java file I am getting the below error. I think it has to something to do with makin classes available to java files. I am new to Java , so trying to understand. can someone please help me?

    below is the error...

    Java Code:
    SeleniumTrafficAnalyserExampleTest.java:41: error: cannot find symbol
                    Type collectionOfHTMLRequestsType = new TypeToken<Collection<HTM
    LRequestFromSelenium>>(){}.getType();
                                                                                 ^
      symbol:   class HTMLRequestFromSelenium
      location: class SeleniumTrafficAnalyserExampleTest
    SeleniumTrafficAnalyserExampleTest.java:42: error: cannot find symbol
                    Collection<HTMLRequestFromSelenium> seleniumRequests = gson.from
    Json(trafficOutput, collectionOfHTMLRequestsType);
                               ^
      symbol:   class HTMLRequestFromSelenium
      location: class SeleniumTrafficAnalyserExampleTest
    SeleniumTrafficAnalyserExampleTest.java:42: error: incompatible types
                    Collection<HTMLRequestFromSelenium> seleniumRequests = gson.from
    Json(trafficOutput, collectionOfHTMLRequestsType);

    below is the Code in SeleniumTrafficAnalyserExampleTest.java



    Java Code:
    package com.eviltester.captureNetworkTraffic;
    
    import static org.junit.Assert.*;
    import java.lang.reflect.Type;
    import java.util.ArrayList;
    import java.util.Collection;
    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.Set;
    import org.junit.Test;
    import org.openqa.selenium.server.SeleniumServer;
    import com.google.gson.Gson;
    import com.google.gson.Gson.*;
    import com.google.gson.reflect.*;
    import com.thoughtworks.selenium.DefaultSelenium;
    
    public class SeleniumTrafficAnalyserExampleTest {
    	
    	@Test
    	public void testProfileEvilTester() throws Exception{
    		
    		// Start the Selenium Server
    		SeleniumServer srvr = new SeleniumServer();
    		srvr.start();
    			
    		// Create a Selenium Session with captureNetworkTraffic ready
    		String site = "http://www.eviltester.com";
    		
    		DefaultSelenium selenium = new DefaultSelenium("localhost", 4444, "*firefox", site);
    		selenium.start("captureNetworkTraffic=true");
    		
    		// open a page to get the traffic
    		selenium.open("/");
    			
    		// dump the traffic into a variable in Json format
    		String trafficOutput = selenium.captureNetworkTraffic("json");
    		System.out.println(trafficOutput);
    
    		// parse the json using Gson
    		Gson gson = new Gson();
    		Type collectionOfHTMLRequestsType = new TypeToken<Collection<HTMLRequestFromSelenium>>(){}.getType();
    		Collection<HTMLRequestFromSelenium> seleniumRequests = gson.fromJson(trafficOutput, collectionOfHTMLRequestsType);
    		
    		// get ready to analyse the traffic
    		TrafficAnalyser ta = new TrafficAnalyser(seleniumRequests);
    		
    		// this is pretty much copied from Corey's python example
    		int num_requests = ta.get_num_requests(); 
    		int total_size = ta.get_content_size(); 
    		HashMap<Integer,Integer> status_map = ta.get_http_status_codes(); 
    		HashMap<String, Object[]> file_extension_map = ta.get_file_extension_stats(); 
    				      
    		     System.out.println("\n\n--------------------------------"); 
    		     System.out.println(String.format("results for %s",site)); 
    		     System.out.println(String.format("content size: %d kb",total_size)); 
    		     System.out.println(String.format("http requests: %d",num_requests));
    		     
    		     Iterator<Integer> statusIterator = status_map.keySet().iterator() ; 
    		     while ( statusIterator.hasNext (  )  )  
    		      {  
    		    	 int key = statusIterator.next();
    		    	 System.out.println(String.format("status %d: %d", key, status_map.get(key)));
    		      } 
    		      
    		     System.out.println("\nfile extensions: (count, size)"); 
    		     Iterator<String> extensionIterator = file_extension_map.keySet().iterator() ; 
    		     while ( extensionIterator.hasNext (  )  )  
    		      {  
    		    	 String key = extensionIterator.next();
    		    	 System.out.println(String.format("%s: %d, %f", key, file_extension_map.get(key)[0],file_extension_map.get(key)[1]));
    		      } 
    		     		          		
    		    System.out.println("\nhttp timing detail: (status, method, url, size(bytes), time(ms))");
    			for (Iterator iterator = seleniumRequests.iterator(); iterator.hasNext();) {
    					HTMLRequestFromSelenium hr = (HTMLRequestFromSelenium) iterator.next();
    					//totalContentSize += hr.bytes;
    					System.out.println(String.format("%d, %s, %s, %d, %d",hr.statusCode, hr.method, hr.url, hr.bytes, hr.timeInMillis));
    			}
    		     
    		// close everything down
    		selenium.close();
    		selenium.stop();
    		srvr.stop();
    	}
    
    }


    below is the Code in TrafficAnalyser.java



    Java Code:
    package com.eviltester.captureNetworkTraffic;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.util.Collection;
    import java.util.HashMap;
    import java.util.Iterator;
    
    public class TrafficAnalyser {
    
    	private Collection<HTMLRequestFromSelenium> seleniumRequests;
    	
    	public TrafficAnalyser(Collection<HTMLRequestFromSelenium> seleniumRequests) {
    		this.seleniumRequests = seleniumRequests;
    	}
    
    	public int get_num_requests() {
    		return seleniumRequests.size();
    	}
    
    	public int get_content_size() {
    		int totalContentSize = 0;
    		
    		for (Iterator iterator = seleniumRequests.iterator(); iterator.hasNext();) {
    			HTMLRequestFromSelenium hr = (HTMLRequestFromSelenium) iterator.next();
    			totalContentSize += hr.bytes;
    		}
    		
    		return totalContentSize;
    	}
    
    	public HashMap<Integer,Integer> get_http_status_codes() {
    		HashMap<Integer,Integer> statusCodes = new HashMap<Integer,Integer>();
    		
    		for (Iterator iterator = seleniumRequests.iterator(); iterator.hasNext();) {
    			HTMLRequestFromSelenium hr = (HTMLRequestFromSelenium) iterator.next();
    			if(statusCodes.containsKey(hr.statusCode)){
    				statusCodes.put(hr.statusCode, statusCodes.get(hr.statusCode)+1);
    			}else{
    				statusCodes.put(hr.statusCode, 1);
    			}
    		}
    		
    		return statusCodes;
    	}
    
    	public HashMap get_file_extension_stats() {
    		HashMap<String,Object[]> extensions = new HashMap<String,Object[]>();
    		
    		for (Iterator iterator = seleniumRequests.iterator(); iterator.hasNext();) {
    			HTMLRequestFromSelenium hr = (HTMLRequestFromSelenium) iterator.next();
    			URL url = null;
    			try {
    				url = new URL(hr.url);
    				String file_extension;
    				
    				double size = hr.bytes/1000.0;
    				
    				file_extension="";
    				String doc = url.getPath();
    				if(doc.contains("."))
    					file_extension = doc.substring(doc.indexOf(".")+1).trim();
    
    				if(file_extension.compareTo("")==0)
    					file_extension = "unknown";
    				
    				if(extensions.containsKey(file_extension)){
    					Object[] stats = extensions.get(file_extension);
    					stats[0] = (Integer)stats[0] +1;
    					stats[1] = (Double)stats[1] + size;
    					extensions.put(file_extension, stats);
    				}else{
    					Object[] stats = new Object[2];
    					stats[0] = 1;
    					stats[1] = size;
    					extensions.put(file_extension, stats);
    				}
    				
    			} catch (MalformedURLException e) {
    			}
    		}
    		
    		return extensions;
    	}
    
    }


    below is the code in HTMLRequestFromSelenium.java


    Java Code:
    package com.eviltester.captureNetworkTraffic;
    
    import java.util.List;
    
    public class HTMLRequestFromSelenium {
    
    	public int statusCode;
    	public String method;
    	public String url;
    	public int bytes;
    	public String start;
    	public String end;
    	public int timeInMillis;
    	public List<ValuePair> requestHeaders;
    	
    }

    below is the code in ValuePair.java


    Java Code:
    package com.eviltester.captureNetworkTraffic;
    
    public class ValuePair {
    
    	private String name;
    	private String value;
    }

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default Re: compilation error - pls help

    How did you compile this?
    Full command, and location of files (and the directory you are working from).

    I do have to ask, if you are so new to Java that you don't entirely understand that error then what on earth are you doing working with Selenium?
    DarrylBurke likes this.

  3. #3
    codejava is offline Member
    Join Date
    Sep 2011
    Posts
    6
    Rep Power
    0

    Default Re: compilation error - pls help

    Hello Tolls..
    Thanks for commenting on my post.
    I've worked with selenium before but with Perl. I am trying to capture the network traffic with selenium. I did not write those scripts.I got it from the web. I'll try to modify them afterwards. I am new to Java hence facing difficulties.

    working directory: C:\Documents and Settings\chauhanamit\Desktop\amit\selenium\capture NetworkTraffic\src\com\eviltester\captureNetworkTr affic

    command: C:\Documents and Settings\chauhanamit\Desktop\amit\selenium\capture NetworkTraffc\src\com\eviltester\captureNetworkTra ffic>javac SeleniumTrafficAnalyserExampleest.java

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default Re: compilation error - pls help

    I'm guessing it's a classpath issue.
    Try:
    javac -cp ../../.. SeleniumTrafficAnalyserExampleTest.java

    That should be sufficient .. to get it up to the src directory.
    That way java knows where the top of the package structure is.

  5. #5
    codejava is offline Member
    Join Date
    Sep 2011
    Posts
    6
    Rep Power
    0

    Default Re: compilation error - pls help

    I was trying to run javac -cp ../../.. SeleniumTrafficAnalyserExampleTest.java but it keeps on giving me "file not found"

    then I ran

    javac -classpath . SeleniumTrafficAnalyserExampleTest.java

    from working directory

    but got errors related to classpath that org.junit does not exist , probably because it took the classpath as my current directory and did not look for files in my classpath where all the junit jars are there.

    also, just want you to know that all this time(when i was runnig command ) my working directory is included in the classpath.

    any idea how can i resolve this issue?

    Thanks.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default Re: compilation error - pls help

    (Sorry forgot the . in the classpath).

    Do not rely on an environment classpath.
    That is not good practice.

    You should use a classpath specific to your current application.
    If you have a bunch of dependent jar files then stick them in a lib directory at the same level as your src directory and do:
    javac -cp <path to lib>/*;<path to src directory>;. SeleniumTrafficAnalyserExampleTest.java

  7. #7
    codejava is offline Member
    Join Date
    Sep 2011
    Posts
    6
    Rep Power
    0

    Default Re: compilation error - pls help

    Hello..

    After a lot of effort , I managed to resolve all the compilation errors and tried to run the program.

    This is the command I used for compiling.

    javac -d bin -sourcepath src -classpath lib\junit-4.9.jar;lib\selenium-java-2.5.0.jar;lib\gson-1.7.1.jar;lib\selenium-java-2.5.0-srcs.jar;lib\guava-r09.jar;lib\selenium-server-standalone-2.4.0.jar src\com\eviltester\captureNetworkTraffic\SeleniumT rafficAnalyserExampleTest.java

    and below is the command that I used to run the program

    java -classpath bin\com\eviltester\captureNetworkTraffic;lib\junit-4.9.jar;lib\selenium-java-2.5.0.jar;lib\gson-1.7.1.jar;lib\selenium-java-2.5.0-srcs.jar;lib\guava-r09.jar;lib\selenium-server-standalone-2.4.0.jar;com.eviltester.captureNetworkTraffic.Sel eniumTrafficAnalyserExampleTest

    but I am getting error :

    Error: Could not find or load main class com.eviltester.captureNetworkTraffic.SleniumTraffi cAnalyserExampleTest

    Is it because theres no main method in SleniumTrafficAnalyserExampleTest.java file? how can I include this method? I've mentioned the code above for this file. Can you or someone please help?

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default Re: compilation error - pls help

    You can shorten:
    -classpath lib\junit-4.9.jar;lib\selenium-java-2.5.0.jar;lib\gson-1.7.1.jar;lib\selenium-java-2.5.0-srcs.jar;lib\guava-r09.jar;lib\selenium-server-standalone-2.4.0.jar
    to:
    -classpath lib\*
    for javac and:
    -classpath bin\com\eviltester\captureNetworkTraffic;lib\*
    for java, which highlights your problem. Your bin directory is wrong. You are referring to the bottom package, whereas your class (as shown by the error message) is in:
    com.eviltester.captureNetworkTraffic
    package. So the classpath for java should actually be:
    -classpath bin;lib\*
    And yes, your class should have a main() method as well, if it doesn't already.

  9. #9
    codejava is offline Member
    Join Date
    Sep 2011
    Posts
    6
    Rep Power
    0

    Default Re: compilation error - pls help

    can you please provide me an idea on how can i include

    public static void main(String args[]) {
    }

    in SeleniumTrafficAnalyserExampleTest.java

    Thanks!!

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default Re: compilation error - pls help

    Add one to the code then recompile?

    Oh, hang on. That's a JUnit test isn't it?
    You need to be running Junit to actually run that test.
    The JUnit docs'll tell you how.
    It'll be something like:
    java -cp <the above classpath> <the JUnit entry class, which I think is JUnitCore?> <your test class, ie SeleniumTrafficAnalyserExampleTest>

  11. #11
    codejava is offline Member
    Join Date
    Sep 2011
    Posts
    6
    Rep Power
    0

    Default Re: compilation error - pls help

    Thanks! for all your posts
    I was able to compile and run the program.
    We need to add the following code at the bottom of the code within the class

    Java Code:
    public static junit.framework.Test suite() {
    return new TestSuite(SeleniumTrafficAnalyserExampleTest.class);
    }
    
    public static void main(String[] args) {
    junit.textui.TestRunner.run(suite());
    }

    and extend the main class like like this

    Java Code:
    public class SeleniumTrafficAnalyserExampleTest extends TestCase{...}
    to be able to run as junit test case.

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default Re: compilation error - pls help

    Depends on the version of JUnit.
    The newer ones don't require you to extend TestCase, or provide a main() method.
    You just need to annotate your test methods with @Test (as you have done).
    Since you're using annotation I suspect you are on the newer version.

Similar Threads

  1. Help with a compilation error
    By m_mccutcheon in forum New To Java
    Replies: 5
    Last Post: 08-15-2011, 05:20 PM
  2. compilation error
    By NoufalpRahman in forum Java Servlet
    Replies: 0
    Last Post: 04-26-2011, 08:08 PM
  3. Classes, compilation error
    By l flipboi l in forum New To Java
    Replies: 2
    Last Post: 02-11-2011, 07:38 PM
  4. bean compilation error
    By technical_helps@yahoo.com in forum Enterprise JavaBeans (EJB)
    Replies: 0
    Last Post: 07-29-2009, 11:21 PM
  5. compilation error with Jcreator
    By Heather in forum JCreator
    Replies: 2
    Last Post: 06-30-2007, 04:12 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •