Solved

Problem with import while compiling with javac

Posted on 2015-01-22
4
236 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
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…

770 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