Dumb Q: Why import doesn't work?

Discussion in 'App Development' started by silveredge, Oct 25, 2019.

    • I have a class file Sort.class located in Dropbox/Java/Data/Organize
    • in it, package Data.Organize;
    • classpath (environment var in Windows) set to Dropbox/Java
    When i try to use that Sort class by import Data.Organize.Sort, it reads "Static Error: Undefined class" in drJava. can you shed light on the issue? I believe JVM starts searching at Dropbox/Java, then goes to the package name Data.Organize by replacing "." with "/" so should import Dropbox/Java/Data/Organize/Sort.class, where is my misunderstanding?

    Thanks.
     
  1. gaussian

    gaussian

    Classpath issues are miserable. That would be my guess.

    Try firing it up in an editor like IntelliJ IDEA and let it sort out the classpath for you. More saliently, use Gradle as your launcher. It will make life significantly easier.

    It'd be easier if you posted code/project layout. You can do this in mac/linux using `tree` so we can see the structure, and the related code so we can understand the path.
     
    silveredge likes this.
  2. If you were to use Netbeans as your IDE I think that you should add Organize as a library to your project. I'm not sure whether the IDE you use has this done automatically, or whether a manual action is required.
     
    silveredge likes this.
  3. i still use drJava since college but will gradually graduate to IntelliJ. I am still learning and experimenting basic syntax and mostly write a small class and import it into drJava's Interaction Pane, which allows you to type lines of Java and to have them interpreted interactively. This lets you create objects and play around with them without having to write a main program.

    I only know the basics of classpath and they look fine in system environment variables.
    Command prompt javac etc all work but when I try to compile the same file in Drjava, it says it can't find package java.lang in classpath; but i can import java.util.* etc which should be in the same path.

    1. I wonder doesn't an IDE find its classpath automatically from system environment variables so as long as command prompt works, it should work?

    2. What other IDEs have the Interaction Pane?
    https://books.trinket.io/thinkjava2/appendix-a.html#sec173
    http://ice-web.cc.gatech.edu/ce21/1/static/JavaReview-RU/Appendix/DrJava.html
     
    Last edited: Oct 26, 2019
  4. Consider this discussion here which could be relevant: https://stackoverflow.com/questions...ckage-java-lang-in-classpath-or-bootclasspath

    Generally speaking you could try to be a bit more helpful and informative when seeking assistance:
    • No verbatim copy of the classpath - not just the env. var. but perhaps also that which might be set in your IDE and in anycase what the final classpath path is at runtime .e.g what is the output of the following:
    Code:
    String classpath = System.getProperty("java.class.path");
    String[] classpathEntries = classpath.split(File.pathSeparator);
    and what is 'Dropbox/Java', a folder off the drive root e.g. C:\ or a relative path - if relative, to where? ok on the command prompt current working directory where your code is but how does the IDE resolve it?​
    • No copy of your existing import statements including the one referencing Sort (so we can reason if you're right about where the code is picking up its imports from)
    • What package is this class trying to use Sort in
    • Which version of JDK (javac) you are using on the command line and is JAVA_HOME env. var. set and what to
    • Which version of JDK is DrJava pointing to or supposed to be using (you may have multiple versions installed)
    IDEs build up the classpath based on the code and dependencies they are compiling (compilation classpath) or running (runtime classpath). They extend any existing classpath information in env. vars. with constructs such as classpath=%classpath%+path1;path2;jar1;jar2 and so on.​

    Hopefully you've already sorted everything out since posting.
     
    Last edited: Oct 27, 2019