0

So, I have a package "com", which consists of two sub-packages "Common" and "Model1". The Model1 contains a class Model which I am trying to import in the Servlet2 class, which resides in the Common package. I compile the Model class first which stays fine, but the Servlet2 class doesn't and comes up with an error saying "package com.Model1 doesn't exist"

Here's the Model class:

 package com.Model1;
    import java.util.*;
   **public** class Model{
        public ArrayList<String> getBrands(String color){
            ArrayList<String> brands=new ArrayList<String>();
            if(color.equals("amber")){
                brands.add("Jack Amber");
                brands.add("Red Moose");
            }
            else{
                brands.add("Jail Pale Ale");
                brands.add("Gout Stout");
            }
            return brands;
        }
    }

Here's the Servle2 class:

package com.Common;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import com.Model1.Model;
public class Servlet2 extends HttpServlet{
    public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException{
        res.setContentType("text/html");
        PrintWriter out=res.getWriter();
        out.println("Coffee selection advice<br>");
        String c=req.getParameter("color");
        Model m=new Model();
        ArrayList result=m.getBrands(c);
        out.println("<br>Got coffee color "+c);
        Iterator it=result.iterator();
        while(it.hasNext()){
            out.println("<br> Try: "+it.next());
        }
    }
}

I just can't seem to figure out how to sort this out. Edit: Realised that default modifier is restrictive, but even making it public doesn't seem to work.

I am using notepad++ and I hope this works as the Minimal, Complete, and Verifiable example:

package com.common;
import com.model.*;
public class TheClassIWantToImportInto {
public static void main(String[] args){
System.out.println("Testing 1");
TheClassIwantToImport obj=new TheClassIWantToImportInto();
obj.testFunction();
}
}

The second class:

package com.model;
public class TheClassIWantToImport{
public void testFunction(){
System.out.println("testing function");
}
}

Both the .java files are in the same folder "C:\Program Files\Java\jdk1.8.0_161\bin"

Using the following commands in this order: javac -d . TheClassIWantToImport.java (Works fine) javac -d . TheClassIWantToImportInto.java (Error: package com.model doesn't exist)

NoOb121
  • 29
  • 5
  • You can't import non-public class. See [this answer](https://stackoverflow.com/a/215505/7470253) for more details. – M. Prokhorov Sep 10 '18 at 11:23
  • Well, I tried making the Model class public now, compiled it first and then tried compiling the Servlet2... Still the same error. – NoOb121 Sep 10 '18 at 11:27
  • Oh, it looks like I misread your error. It seems that it wasn't about fact that your application couldn't find `Model` *within* `com.Model1`, but rather that it can't find `com.Model1` itself. But to help you with that we would need some [mcve] (code and/or instructions which would let us reproduce your problem, like what is your IDE, project structure, server, and so on). – Pshemo Sep 10 '18 at 11:35
  • 1
    Please stick to Java code conventions and write package names _lowercase_: `com.model1` for example. Some compilers might think that `Model1` is a class rather than a package. – Seelenvirtuose Sep 10 '18 at 11:37
  • 1
    Compile both classes at once. Or use a real build tool, like gradle or Maven. – JB Nizet Sep 10 '18 at 11:38
  • Possibly related: [How to compile and run with this folder structure](https://stackoverflow.com/q/18740468) – Pshemo Sep 10 '18 at 18:01

1 Answers1

0
  1. Set up your project such that you have some root dir which is your project's main directory. Let's call that /Users/you/projects/MyCoolProject

  2. Within that, make a src/main dir which will contain your sources. A source file goes in a directory that matches its package declaration. com.Model1 is a bad package name for three reasons. Convention states not to start them with caps, convention states that they are supposed to represent either your reverse website or failing that, at least the project name, and finally 'model1' is not descriptive. So let's go with package com.mycompany.coolproject.vehicleModel; instead. In that case, your Model class should be on your disk at /Users/you/projects/MyCoolProject/src/main/com/mycompany/coolproject/vehicleModel/Model.java

  3. Use a build tool such as maven or gradle to build your project. This is going to be a lot simpler than trying to manually make javac do the right thing here. If you MUST use java, make dir build in /users/you/projects/MyCoolProject, make sure you're in that directory, and then try: javac -d build -sourcepath src/main src/main/com/mycompany/coolproject/vehicleModel/*.java and note that you'll have to add a path to that every time you make another package (to avoid having to do that... use maven or gradle).

Once you've done that, this error goes away (the error indicates that javac can't find the other source file because your project isn't properly set up yet. The above instructions lead to a properly set up project, with javac/maven/gradle being capable of finding all your source files as needed, and it's how almost all java programmers work).

rzwitserloot
  • 85,357
  • 5
  • 51
  • 72