Solved

Problem with import while compiling with javac

Posted on 2015-01-22
4
227 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
Comment Utility
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
Comment Utility
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
Comment Utility
Yet another proof how oversight of little mistake can create problems.
0
 
LVL 14

Expert Comment

by:CPColin
Comment Utility
No problem. Happy to be another set of eyes!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

763 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

7 Experts available now in Live!

Get 1:1 Help Now