*
The moose likes Ant, Maven and Other Build Tools and the fly likes Beginner at Ant and Servlets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Ant, Maven and Other Build Tools
Bookmark "Beginner at Ant and Servlets" Watch "Beginner at Ant and Servlets" New topic
Author

Beginner at Ant and Servlets

Ken Rubin
Ranch Hand

Joined: Mar 08, 2004
Posts: 66
Hello,

I am trying to build a Servlet which also uses a library file to retrieve data from MySql and I have trouble building it. At this point I am just taking the step to compile the Servlet and library file.

My build directory structure has a source and class under a common directory called "coins":

Source Directory Structure
coins
src
com
example
model- This has a program Testnew.java with a package of com.example.model for the database code.
At the same level of the model directory, there is a web directory with a package of com.example.web for the Servlet.

The classes directories are under "coins" directory as follows :

coins\classes\com\example\model and coins\classes\com\example\web

My ant file is:
<?xml version="1.0"?>
<project name="test" default="compile" basedir=".">



<property name="src" value="src"/>
<property name="build" value="classes"/>



<target name="compile">
<!-- Compile the java code -->
<javac srcdir="${src}" destdir="${build}"/>
</target>
</project>

My Web code is :
package com.example.web;

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

public class CoinSelect extends HttpServlet
{
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws IOException,ServletException
{

String Series = request.getParameter("Series");
String Grade = request.getParameter("Grade");
String PriceRange = request.getParameter("PriceRange");

result = getCoins(Series,Grade,PriceRange);

request.setAttribute("Coins",result);

RequestDispatcher view = request.getRequestDispatcher("Result.jsp");

view.forward(request,response);
}
}


My SQL code is:
package com.example.model;
import java.sql.* ;

public class Testnew
{

public static void main( String[] args )
{
Testnew query = new Testnew();
query.getResult();

}
public ResultSet getResult()
{
ResultSet rs=null;
try
{
Class.forName( "com.mysql.jdbc.Driver" ).newInstance();
try
{
//Connection con = DriverManager.getConnection( "jdbc:mysql://localhost/soup", "root", null );
Connection con = DriverManager.getConnection( "jdbc:mysql://localhost/coinsystem", "root", "mallory1" );
try
{
Statement statement = con.createStatement();

rs = statement.executeQuery("SELECT POP.ISSUEDATE,PRICE.ISSUEDATE,PRICE.VERYFINE,POP.VERYFINE FROM SEATED_DIME_PRICES PRICE, SEATED_DIME_POPULATION POP WHERE POP.ISSUEDATE = PRICE.ISSUEDATE");

while ( rs.next() )
{
System.out.println( rs.getString( 1 ) + " " + rs.getFloat(3));
}
rs.close();
statement.close();
}
catch ( SQLException e )
{
System.out.println( "JDBC error: " + e );
}
finally
{
con.close();
return rs;
}
}
catch( SQLException e )
{
System.out.println( "could not get JDBC connection: " + e );
}
}
catch( Exception e )
{
System.out.println( "could not load JDBC driver: " + e );
}
return rs;
}
}

My error is as follows. I don't know why it can't see my package for model.


Buildfile: build.xml

init:

compile:
[javac] Compiling 1 source file to C:\web_components\coins\classes\com\examp
le\web
[javac] C:\web_components\coins\src\com\example\web\CoinSelect.java:3: packa
ge com.example.model does not exist
[javac] import com.example.model.*;
[javac] ^
[javac] C:\web_components\coins\src\com\example\web\CoinSelect.java:20: cann
ot find symbol
[javac] symbol : variable result
[javac] location: class com.example.web.CoinSelect
[javac] result = getCoins(Series,Grade,PriceRange);
[javac] ^
[javac] C:\web_components\coins\src\com\example\web\CoinSelect.java:20: cann
ot find symbol
[javac] symbol : method getCoins(java.lang.String,java.lang.String,java.lan
g.String)
[javac] location: class com.example.web.CoinSelect
[javac] result = getCoins(Series,Grade,PriceRange);
[javac] ^
[javac] C:\web_components\coins\src\com\example\web\CoinSelect.java:22: cann
ot find symbol
[javac] symbol : variable result
[javac] location: class com.example.web.CoinSelect
[javac] request.setAttribute("Coins",result);
[javac] ^
[javac] 4 errors





Any help would be greatly appreciated

Thanks,Ken
Peter Johnson
author
Bartender

Joined: May 14, 2008
Posts: 5843
    
    7

Because of the poor formatting of your post, I cannot tell where key files are located. The error message gives the full path for the one file being compiled:

C:\web_components\coins\src\com\example\web\CoinSelect.java

Because of this output:

[javac] Compiling 1 source file to C:\web_components\coins\classes\com\example\web

I deduct that there is only one Java source file within C:\web_components\coins\src (assuming that build.xml is located within C:\web_components\coins).

Where, exactly (full path, please), are the source files for the com.example.model classes?

Also, note this error:

[javac] C:\web_components\coins\src\com\example\web\CoinSelect.java:20: cannot find symbol
[javac] symbol : variable result
[javac] location: class com.example.web.CoinSelect
[javac] result = getCoins(Series,Grade,PriceRange);
[javac] ^

I will leave it to you to find out what is wrong here.


JBoss In Action
Ken Rubin
Ranch Hand

Joined: Mar 08, 2004
Posts: 66
Peter,

Thank you for responding:

Model Directory
c:/web_components/coins/src/com/example/model/
file: Testnew.java

Web Directory
c:/web_components/coins/src/com/example/web/
file:CoinSelect.java

Regards and Thanks,

Ken
Peter Johnson
author
Bartender

Joined: May 14, 2008
Posts: 5843
    
    7

That makes no sense. Ant clearly said that there was only one source file to be compiled:

[javac] Compiling 1 source file to C:\web_components\coins\classes\com\example\web

Yet you claim that there are two source files.

Wait a minute. That Ant output does not match the build script you posted. The build script says that the srcdir for the javac task is .../coins/src, yet the Ant output claims that the srcdir for the javac task is C:\web_components\coins\classes\com\example\web. I think that you posted the wrong files.

Also, the ant script posted is missing the classpath that will include the servlet jar file.
Ken Rubin
Ranch Hand

Joined: Mar 08, 2004
Posts: 66
Peter,

Sorry for the confusion. There were 2 files compiled. I cut off the first.

I have figured the problem out. Have just changed my srcdir= to "src" and my destdir to "classes". Thanks for tip on finding jar file.

Thanks,

Ken
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Beginner at Ant and Servlets