Click here to Skip to main content
15,891,657 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Here's the folder structure:

C:.
└───src
    ├───main
    │   └───java
    │       └───com
    │           └───myCompany
    │               └───Runner.java
    │
    └───test
        └───java
            └───com
                └───myCompany
                    └───BasicTest.java


Here's BasicTest.java:

package com.myCompany;

import junit.framework.TestCase;

public class BasicTest extends TestCase {
	
  public BasicTest() {
  }	  
	
  public void testTrue() {
    assertTrue(true);
  }
}


Here's Runner.java:

package com.myCompany;

public class Runner {

public static void main(String[] args) {

    BasicTest bt = new BasicTest();
    bt.testTrue();

    System.out.print("Finished");
  }
}


Simple enough, right? Read on...

What I have tried:

Time to compile BasicTest.java

C:\workspace\BasicTest>javac -cp c:\junit\latest\junit-4.10.jar c:\workspace\BasicTest\src\test\java\com\myCompany\*.java


This works just fine...

When I try to compile Runner.java, I get this:

C:\workspace\BasicTest>javac -cp c:\workspace\BasicTest\src\test\java\com\myCompany c:\workspace\BasicTest\src\main\java\com\myCompany\*.java


c:\workspace\BasicTest\src\main\java\com\myCompany
c:\workspace\BasicTest\src\main\java\com\myCompany\Runner.java:7: error: cannot find symbol
    BasicTest bt = new BasicTest();
    ^
  symbol:   class BasicTest
  location: class Runner
c:\workspace\BasicTest\src\main\java\com\myCompany\Runner.java:7: error: cannot find symbol
    BasicTest bt = new BasicTest();
                       ^
  symbol:   class BasicTest
  location: class Runner
2 errors


I've googled until I'm blue in the face and cannot figure this error out. I understand the error, it's elementary enough, what I'm trying to say is I don't understand why I'm getting it -- I explicitly gave the compiler the paths.
Posted
Updated 20-Apr-18 8:10am
v4

I have been playing around with this, and reading Setting the class path[^], and managed to get a set built, although not the complete directory tree that you have. Try the following, with the reduced classpath tree:
javac -cp c:\workspace\BasicTest\src\test\java c:\workspace\BasicTest\src\main\java\com\myCompany\*.java


[edit]
Because (I think) BasicTest is (implied to be) in package com.myCompany the compiler and runtime will search for it in %CLASSPATH%\com\myCompany, hence your problems.
[/edit]
 
Share this answer
 
v2
Comments
mgoblue0970 20-Apr-18 14:11pm    
Ooops... I think I meant to reply to your post @Richard MacCutchan but I submitted an answer instead. Please read my reply below. Thanks!
@Richard MacCutchan, thanks for the reply!

I'm really confused by a couple of things though...

Quote:
Because (I think) BasicTest is (implied to be) in package com.myCompany


It's not implied to be. It is! Both source files start with:

package com.myCompany;


Quote:
Try the following, with the reduced classpath tree:
Hide Copy Code
javac -cp c:\workspace\BasicTest\src\test\java c:\workspace\BasicTest\src\main\java\com\myCompany\*.java


That by itself throws a mess of errors... it's not taking into account the junit jar.

When I modify your suggestion like so:

javac -cp c:\junit\latest\junit-4.10.jar;c:\workspace\BasicTest\src\test\java c:\workspace\BasicTest\src\main\java\com\myCompany\*.java

It works!!! Thank-you.

But this still isn't solved yet. It compiles -- but I cannot run:

C:\workspace\BasicTest\src\main\java>java com.myCompany.Runner<br />
Exception in thread "main" java.lang.NoClassDefFoundError: com/myCompany/BasicTest<br />
        at com.myCompany.Runner.main(Runner.java:7)<br />
Caused by: java.lang.ClassNotFoundException: com.myCompany.BasicTest<br />
        at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:582)<br />
        at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:185)<br />
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:496)<br />
        ... 1 more
 
Share this answer
 
Comments
Richard MacCutchan 20-Apr-18 14:26pm    
You must add the same classpath parameters as you did for the Compile phase.
Richard MacCutchan 20-Apr-18 14:28pm    
As to your first point, BasicTest is implied to be in com.myCompany when you try to compile Runner.java. Because at that point the compiler does not know where it is, so it needs the classpath to go and find the class.
mgoblue0970 20-Apr-18 17:07pm    
Thanks! That works. Didn't realize that cp was needed for the running it; just assumed compiling. Between becoming dependent upon an IDE and using an interpreted language, I have a bit to brush up on!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900