Solved

Problem with import while compiling with javac

Posted on 2015-01-22
4
245 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

733 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