Solved

Problem with import while compiling with javac

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

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Eclipse with gitlab 1 79
arguments to jar 5 35
ejb mdb examples 1 8
web project error add remove 1 29
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
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…

829 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