Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: I have a question about import statements

  1. #1
    Junior Member
    Join Date
    Aug 2013
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default I have a question about import statements

    My compile gives error

    --- Update ---

    javac Scoring.java

    Scoring.java:37: error: '.' expected
    import ScoringRequest;
    ^
    Scoring.java:37: error: ';' expected
    import ScoringRequest;
    ^
    Scoring.java:38: error: class, interface, or enum expected
    import ScoringResponse;
    ^
    3 errors


    package Scoring;
     
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.OutputStreamWriter;
    import java.io.PrintWriter;
    import java.io.StringWriter;
     
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
     
    import ScoringRequest;
    import ScoringResponse;
     
    @WebServlet( name="Scoring", displayName="Scoring Servlet", urlPatterns = {"/Scoring"}, loadOnStartup=1)
    public class Scoring extends HttpServlet {
     
    	protected void doPost(HttpServletRequest request,
    			HttpServletResponse response) throws ServletException, IOException {
    		BufferedReader reader = null;
    		BufferedWriter writer = null;
    		ScoringRequest req = null;
     
    		try {
    			reader = new BufferedReader(new InputStreamReader(
    					request.getInputStream()));
     
    			StringBuffer xml = new StringBuffer();
    			String line = reader.readLine();
    			while (line != null) {
    				xml.append(line);
    				line = reader.readLine();
    			}
     
    			req = new ScoringRequest(xml.toString(), null, null,
    					null);
     
    			ScoringResponse res = (new ScoringEngine()).score(req);
     
    			writer = new BufferedWriter(new OutputStreamWriter(
    					response.getOutputStream()));
    			writer.write(res.toXML());
    			writer.flush();
     
    		} catch (Exception ex) {
    			System.err.println(ex);
     
    			ScoringResponse res = new ScoringResponse(req.getModelName(), req.getPmmlURL(), req.getCsvInputRows(), null);
    			StringWriter errWriter = new StringWriter();
    			ex.printStackTrace(new PrintWriter(errWriter));
    			res.setErrorMessage(errWriter.toString());
    //			res.setErrorMessage(ex.getMessage());
     
    			writer = new BufferedWriter(new OutputStreamWriter(
    					response.getOutputStream()));
    			writer.write(res.toXML());
    			writer.flush();
     
    		} finally {
    			try {
    				if (reader != null)
    					reader.close();
    			} catch (Exception ex) {
    			}
     
    			try {
    				if (writer != null)
    					writer.close();
    			} catch (Exception ex) {
    			}
     
    		}
    	}
    }
    Last edited by jps; August 27th, 2013 at 10:46 AM. Reason: code tags


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,162
    My Mood
    Hungover
    Thanks
    141
    Thanked 595 Times in 510 Posts

    Default Re: I have a question about import statements

    I don't think you can import from the default package like that. Those classes need to be in a package if you want to use them in a class in a different package.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. The Following User Says Thank You to KevinWorkman For This Useful Post:

    syedk01 (August 26th, 2013)

  4. #3
    Junior Member
    Join Date
    Aug 2013
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: I have a question about import statements

    Thanks for your quick response.
    I have the code in the same directory as in ScoringRequest.class and ScoringResponse.class. How would I go about putting them in a package so I can import them?

  5. #4
    Member
    Join Date
    Jul 2013
    Posts
    219
    Thanks
    0
    Thanked 18 Times in 17 Posts

    Default Re: I have a question about import statements

    Hello.
    Try this please,
    import Scoring.ScoringRequest;
    import Scoring.ScoringResponse;

    Let us know if the problem still persists.

    Syed.

  6. #5
    Junior Member
    Join Date
    Aug 2013
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: I have a question about import statements

    Hello Syed
    I get the following errors. I do have the "source" to ScoringRequest.java" and "ScoringResponse.java". Do you think I need to "build" a "Scoring" package?

    syedk@syedk-ThinkPad-T410:~/PAC/CODE$ javac ScoringRequest.java ScoringRequest.java:26: error: package org.jdom2 does not exist
    import org.jdom2.CDATA;
    ^
    ScoringRequest.java:27: error: package org.jdom2 does not exist
    import org.jdom2.Document;
    ^
    ScoringRequest.java:28: error: package org.jdom2 does not exist
    import org.jdom2.Element;
    ^
    ScoringRequest.java:29: error: package org.jdom2 does not exist
    import org.jdom2.JDOMException;
    ^
    ScoringRequest.java:30: error: package org.jdom2.input does not exist
    import org.jdom2.input.SAXBuilder;
    ^
    ScoringRequest.java:31: error: package org.jdom2.output does not exist
    import org.jdom2.output.XMLOutputter;
    ^
    ScoringRequest.java:51: error: cannot find symbol
    public ScoringRequest(String xml, String endPointURL, String userID, String password) throws JDOMException, IOException {
    ^
    symbol: class JDOMException
    location: class ScoringRequest
    ScoringRequest.java:56: error: cannot find symbol
    SAXBuilder builder = new SAXBuilder();
    ^
    symbol: class SAXBuilder
    location: class ScoringRequest
    ScoringRequest.java:56: error: cannot find symbol
    SAXBuilder builder = new SAXBuilder();
    ^
    symbol: class SAXBuilder
    location: class ScoringRequest
    ScoringRequest.java:57: error: cannot find symbol
    Document doc = builder.build(new StringReader(xml));
    ^
    symbol: class Document
    location: class ScoringRequest
    ScoringRequest.java:139: error: cannot find symbol
    Element root = new Element("scoring_request");
    ^
    symbol: class Element
    location: class ScoringRequest
    ScoringRequest.java:139: error: cannot find symbol
    Element root = new Element("scoring_request");
    ^
    symbol: class Element
    location: class ScoringRequest
    ScoringRequest.java:140: error: cannot find symbol
    root.addContent(new Element("pmml_url").addContent(pmmlURL));
    ^
    symbol: class Element
    location: class ScoringRequest
    ScoringRequest.java:141: error: cannot find symbol
    root.addContent(new Element("model_name").addContent(modelName));
    ^
    symbol: class Element
    location: class ScoringRequest
    ScoringRequest.java:142: error: cannot find symbol
    root.addContent(new Element("csv_input_rows").addContent(new CDATA(csvInputRows)));
    ^
    symbol: class CDATA
    location: class ScoringRequest
    ScoringRequest.java:142: error: cannot find symbol
    root.addContent(new Element("csv_input_rows").addContent(new CDATA(csvInputRows)));
    ^
    symbol: class Element
    location: class ScoringRequest
    ScoringRequest.java:144: error: cannot find symbol
    XMLOutputter serializer = new XMLOutputter();
    ^
    symbol: class XMLOutputter
    location: class ScoringRequest
    ScoringRequest.java:144: error: cannot find symbol
    XMLOutputter serializer = new XMLOutputter();
    ^
    symbol: class XMLOutputter
    location: class ScoringRequest
    ScoringRequest.java:146: error: cannot find symbol
    return serializer.outputString(new Document(root));
    ^
    symbol: class Document
    location: class ScoringRequest


    Thanks

    Syed

  7. #6
    Member
    Join Date
    Jul 2013
    Posts
    219
    Thanks
    0
    Thanked 18 Times in 17 Posts

    Default Re: I have a question about import statements

    Hi.
    I feel like you didn't add the necessary JAR files.
    Are you using any IDE? If so then please add the required JAR files to your project.
    If you working on command prompt then set the CLASSPATH variable to point to the required JARS.

    Syed.

  8. #7
    Junior Member
    Join Date
    Aug 2013
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: I have a question about import statements

    Quote Originally Posted by syedbhai View Post
    Hi.
    I feel like you didn't add the necessary JAR files.
    Are you using any IDE? If so then please add the required JAR files to your project.
    If you working on command prompt then set the CLASSPATH variable to point to the required JARS.

    Syed.
    Hello Syedbhai
    I added the jar files to the directory I am compiling in. I get the same error. I am not using IDE. Since the all the files (CLASS, JAVA and JAR_ are in the same directory - I assume I do not need the classpath command. However, I try with the classpath command but got the same error.
    Regards
    Syed

  9. #8
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,162
    My Mood
    Hungover
    Thanks
    141
    Thanked 595 Times in 510 Posts

    Default Re: I have a question about import statements

    Quote Originally Posted by syedk01 View Post
    Thanks for your quick response.
    I have the code in the same directory as in ScoringRequest.class and ScoringResponse.class. How would I go about putting them in a package so I can import them?
    If the classes are all in the same package, you don't have to import them.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  10. #9
    Member
    Join Date
    Jul 2013
    Posts
    219
    Thanks
    0
    Thanked 18 Times in 17 Posts

    Default Re: I have a question about import statements

    Hello.
    If you working with the command prompt then you need to first set the CLASSPATH.
    And then you need to compile cleverly so that packages get generated appropriately.
    Syed.

    --- Update ---

    And I dont' believe everything (java files, class files) is in the same directory.
    Because you have package statement in your source code.

    Syed.

Similar Threads

  1. [SOLVED] Which is more efficient? "import java.package.subpackage" or "import java.package.*"
    By Andrew R in forum Java Theory & Questions
    Replies: 1
    Last Post: August 18th, 2013, 01:11 PM
  2. Import if Import is Found, Else
    By blazedGinger in forum Java Theory & Questions
    Replies: 5
    Last Post: March 9th, 2013, 05:43 PM
  3. "The import ___ cannot be resolved" (stupid noob question)
    By RobG in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 18th, 2010, 02:09 PM
  4. import question
    By bardd in forum Java Theory & Questions
    Replies: 1
    Last Post: November 20th, 2010, 08:27 AM
  5. [SOLVED] Do i need to import anything here?
    By straw in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 6th, 2010, 05:42 PM