Solved

Problem with import while compiling with javac

Posted on 2015-01-22
4
232 Views
Last Modified: 2015-01-22
It's been a long time since I compiled java classes in command prompt with javac.
I am following an older book (but a good one) and  I encountered problem with paths while importing another class.

Here is my directory structure:
File structure 01Directory structure 02Directory structure
I have two classes. Nothing special really.
First one is BeerExpert.java and I compile by invoking javac from C:\MyProjects\beerV1 directory such as:
javac -classpath \MyProjects\beerV1\lib\servlet-api.jar;classes:. -d classes src\com\example\model\BeerExpert.java

Open in new window


package com.example.model;
import java.util.*;

public class BeerExpert {
	public List getBrands(String color) {
		List brands = new ArrayList();
			if (color.equals("amber")) {
				brands.add("Jack Amber");
				brands.add("Red Moose");
		}
		else {
		brands.add("Jail Pale Ale");
		brands.add("Gout Stout");
		}
		return(brands);
	}
}

Open in new window


Second one is BeerSelect.java which I also compile by invoking javac from same directory C:\MyProjects\beerV1 with
javac -classpath \MyProjects\beerV1\lib\servlet-api.jar;classes:. -d classes src\com\example\web\BeerSelect.java

Open in new window


package com.example.web;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class BeerSelect extends HttpServlet {
	public void doPost(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException {
			response.setContentType("text/html");
			PrintWriter out = response.getWriter();
			out.println("Beer Selection Advice<br>");
			String c = request.getParameter("color");
			out.println("<br>Got beer color " + c);
	}
}

Open in new window


Those statemets are OK, .java files were found and compiled and their respective .class files are placed where they should be. But when I rewrite BeerSelect.java and add import statement with some additional changes like:

package com.example.web;

import com.example.model.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;

public class BeerSelect extends HttpServlet {
	public void doPost(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException {
			
			String c = request.getParameter(“color”);
			BeerExpert be = new BeerExpert();
			List result = be.getBrands(c);

			response.setContentType("text/html");
			PrintWriter out = response.getWriter();
			out.println("Beer Selection Advice<br>");
			Iterator it = result.iterator();
			while(it.hasNext()) {
			out.print("<br>try: " + it.next());
		}
	}
}

Open in new window


and then try to compile it with:

javac -classpath \MyProjects\beerV1\lib\servlet-api.jar;classes:. -d classes src\com\example\web\BeerSelect.java

Open in new window


I got this error:
C:\MyProjects\beerV1>javac -classpath \MyProjects\beerV1\lib\servlet-api.jar;classes:. -d classes src\com\example\web\BeerSelect.java
src\com\example\web\BeerSelect.java:3: error: package com.example.model does not exist
import com.example.model.*;
^
src\com\example\web\BeerSelect.java:14: error: cannot find symbol
                        BeerExpert be = new BeerExpert();
                        ^
  symbol:   class BeerExpert
  location: class BeerSelect
src\com\example\web\BeerSelect.java:14: error: cannot find symbol
                        BeerExpert be = new BeerExpert();
                                            ^
  symbol:   class BeerExpert
  location: class BeerSelect
3 errors

C:\MyProjects\beerV1>

Open in new window


I don't understand why javac diesn't "see" package com.example.model ?
0
Comment
Question by:KPax
  • 2
  • 2
4 Comments
 
LVL 14

Accepted Solution

by:
CPColin earned 500 total points
ID: 40564988
Looks like your classpath has a semicolon and a colon, instead of two semicolons. What happens when you replace the colon with a semicolon?
0
 

Author Comment

by:KPax
ID: 40565033
Here is what happens when I
replace the colon with a semicolon?
I start feeling like a moron :) :)

You were right, now it compiles OK, thank you!
0
 

Author Closing Comment

by:KPax
ID: 40565037
Yet another proof how oversight of little mistake can create problems.
0
 
LVL 14

Expert Comment

by:CPColin
ID: 40565048
No problem. Happy to be another set of eyes!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now