Note that there are some explanatory texts on larger screens.

plurals
  1. POHelp with packages in java - import does not work
    text
    copied!<p>I'm a C++ developer - not a java developer, but have to get this code working...</p> <p>I have 2 public classes that will be used by another product. I used the package directive in each of the java files. </p> <pre><code>package com.company.thing; class MyClass ... </code></pre> <p>When I try to compile a test app that uses that I add </p> <pre><code>import com.company.thing.*; </code></pre> <p>The javac compiler fails with errors about com.company does not exist. (even if I compile it in the same directory as the class files I just made a package of)</p> <p>I am sure I am doing something bone-headed and silly.</p> <p>I've read the <a href="http://java.sun.com/docs/books/tutorial/java/package/usepkgs.html" rel="noreferrer">http://java.sun.com/docs/books/tutorial/java/package/usepkgs.html</a> pages and tried to set up a directory structure like /com/company/thing etc, but either I have totally screwed it all up or am missing something else.</p> <p>EDIT thanks for the suggestions - I had tried the classpath previously. It does not help.</p> <p>I tried compiling </p> <pre><code>javac -classpath &lt;parent&gt; client.java </code></pre> <p>and the result is:</p> <pre><code>package com.company does not exist </code></pre> <p>I have the code I want to import (the two java files) in \com\company\product. I compile those fine. (they contain MyClass) I even made a jar file for them. I copied the jar file up to the parent directory.</p> <p>I then did (in the parent directory with the client java file) </p> <pre><code>javac -cp &lt;jarfile&gt; *.java </code></pre> <p>the result is:</p> <pre><code>cannot access MyClass bad class file: MyClass.class(:MyClass.class) class file contains wrong class: com.company.product.MyClass Please remove or make sure it appears in the correct subdirectory of the classpath. </code></pre> <p>EDIT</p> <p>I got the client code to compile and run if I used the fully qualified name for MyClass and compiled it in the parent directory. I am totally confused now. </p> <p>compiled with no classpath set - just </p> <pre><code>javac *.java </code></pre> <p>in the parent directory - and it worked fine.</p> <p>I can get a test app to compile, but that is not going to cut it when i have to integrate it into the production code. Still looking for help.</p> <p>EDIT:</p> <p>Finally - not sure why it didn't work before - but I cleaned up all the files all over the directory structure and now it works. </p> <p>Thanks</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload