Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>These examples assume the following source structure:</p> <pre><code>C:\temp\compile-test\src\a\b\c\D.java </code></pre> <p>Where D.java is:</p> <pre><code>package a.b.c; public class D { } </code></pre> <p>The first problem, <strong>cannot read: myfile.java</strong>, is because it is not correct to use the <strong><code>cp</code></strong> command line option to point to your source code.</p> <pre><code>C:\temp\compile-test\src&gt;javac -cp c:\temp\compile-test\src\a\b\c D.java javac: file not found: D.java Usage: javac &lt;options&gt; &lt;source files&gt; use -help for a list of possible options </code></pre> <p>This should instead be the following, where <code>javac</code> is run from your source folder, and we can use relative paths to the source files (NOTE - <code>javac</code> is run from the source folder here):</p> <pre><code>C:\temp\compile-test\src&gt;javac a\b\c\D.java </code></pre> <p>Or this, where we specify full paths to the source files, and <code>javac</code> can be run from anywhere (NOTE - <code>javac</code> is run from <code>C:\</code> here):</p> <pre><code>C:\&gt;javac temp\compile-test\src\a\b\c\D.java </code></pre> <p>Both of the above options will result in your class files being created in the same folder as the source. I.e.:</p> <pre><code>C:\temp\compile-test\src\a\b\c\D.class </code></pre> <p>For the second problem, if you try and run a class that has a package name from 'inside' the package, this will result in the name being wrong (NOTE - <code>java</code> being run from 'inside' the package here):</p> <pre><code>C:\temp\compile-test\src\a\b\c&gt;java D Exception in thread "main" java.lang.NoClassDefFoundError: D (wrong name: a/b/c/D) at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631) at java.lang.ClassLoader.defineClass(ClassLoader.java:615) at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141) at java.net.URLClassLoader.defineClass(URLClassLoader.java:283) at java.net.URLClassLoader.access$000(URLClassLoader.java:58) at java.net.URLClassLoader$1.run(URLClassLoader.java:197) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:190) at java.lang.ClassLoader.loadClass(ClassLoader.java:306) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301) at java.lang.ClassLoader.loadClass(ClassLoader.java:247) Could not find the main class: D. Program will exit. </code></pre> <p>To run the <code>D</code> class, you should be at the package 'root', and supply the <a href="http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Class.html#getName%28%29" rel="noreferrer">Fully Qualified Class Name</a>. I.e.:</p> <pre><code>C:\temp\compile-test\src&gt;java a.b.c.D Exception in thread "main" java.lang.NoSuchMethodError: main </code></pre> <p>Note I get an exception as the <code>D</code> class doesn't have a main method, and so cannot be run. To fix, we add a main method:</p> <pre><code>package a.b.c; public class D { public static void main(String[] args) { System.out.println("main"); } } </code></pre> <p>and re-run:</p> <pre><code>C:\temp\compile-test\src&gt;java a.b.c.D main </code></pre>
 

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