-2

I'm making a PageRank class that relies on some linear algebra operations that I've written myself. Since I couldn't be bothered to learn JUnit testing, I decided to write my own testing class for my PageRank class. Unfortunately, it wasn't as easy as I expected. Here's my basic idea:

pagerank/PageRankTest.java:

package pagerank;

public class PageRankTester{

    public static PageRank pagerank = new PageRank();

    public static void main(String[] args){
        testNorm();
        testSubtract();
        testDot();
        testMatrixMul();
    }

    private static void testNorm(){
        // Tests pagerank.norm()
    }

    private static void testSubtract(){
        // Tests pagerank.subtract()
    }    

    public static void testDot(){
        // Tests pagerank.dot()
    }
    public static void testMatrixMul(){
        // Tests pagerank.matrixMul()
    }
}

pagerank/PageRank.java:

package pagerank;

import java.util.*;
import java.io.*;

public class PageRank {

    public PageRank( String filename ) {
    }
    /* --------------------------------------------- */

    private double[] dot(double[] x, double[][] P){
        //returns dot product between vector and matrix
        return res;
    }

    private double[][] matrixMul(double x, double[][] J){
        //returns x*J
        return J;
    }

    private double[] subtract(double[] x, double[] y){
        //returns elementwise x-y
        return v;
    }

    private double norm(double[] x){
        // returns euclidean norm of x
        return norm;
    }

    public static void main( String[] args ) {
        if ( args.length != 1 ) {
            System.err.println( "Please give the name of the link file" );
        }
        else {
            new PageRank( args[0] );
        }
    }
}

This causes a number of issues.

1: When compiling the test class using javac PageRankTester.java I get the following error:

$ javac PageRankTester.java
PageRankTester.java:4: error: cannot find symbol
    public static PageRank pagerank = new PageRank();
                  ^
  symbol:   class PageRank
  location: class PageRankTester
PageRankTester.java:4: error: cannot find symbol
    public static PageRank pagerank = new PageRank();
                                          ^
  symbol:   class PageRank
  location: class PageRankTester
2 errors

I thought when you put everything in a package you didn't have to import? My only conclusion is that I'm compiling the wrong way because I've worked in packages where I don't need to import anything from another package.

2: Compiling PageRank.java works fine, but when trying to run PageRank using java PageRank I get:

$ java PageRank
Error: Could not find or load main class PageRank
Caused by: java.lang.NoClassDefFoundError: pagerank/PageRank (wrong name: PageRank)

I've tried java -cp . PageRank, to prevent this error, but with no success.

What is the proper way to achieve what I want here?

Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
Sahand
  • 7,980
  • 23
  • 69
  • 137
  • 2
    you should "learn" junit. – luk2302 Feb 24 '18 at 11:35
  • Possible duplicate of [Why am I getting a NoClassDefFoundError in Java?](https://stackoverflow.com/questions/34413/why-am-i-getting-a-noclassdeffounderror-in-java) – xingbin Feb 24 '18 at 11:40
  • Which directory are you compiling from? – jrtapsell Feb 24 '18 at 12:32
  • Learn JUnit testing otherwise your are not progressing with your development skills. Unit tests are not only about running method, they have asserts, expected results and code coverage which are quite important criteria of testing. – Evilguy Feb 24 '18 at 12:35

1 Answers1

0

You have a problem with compiling, start using ide eclipse or intelij Idea.

The PageRank class is not compilable

Another problem is in your package name, try like that:

java pagerank.PageRank

So if you would create a folder structure like that:

src/
  pagerank/
    PageRank.java
    PageRankTester.java

PageRank.java

package pagerank;

public class PageRank {
  public PageRank(String filename) {}


  public static void main(String[] args) {
    if (args.length != 1) {
      System.err.println("Please give the name of the link file");
    } else {
      new PageRank(args[0]);
    }
  }
}

PageRankTester.java

package pagerank;

public class PageRankTester{

    public static PageRank pagerank = new PageRank("filename");

    public static void main(String[] args){
        testNorm();
        testSubtract();
        testDot();
        testMatrixMul();
    }

    private static void testNorm(){
        // Tests pagerank.norm()
    }

    private static void testSubtract(){
        // Tests pagerank.subtract()
    }    

    public static void testDot(){
        // Tests pagerank.dot()
    }
    public static void testMatrixMul(){
        // Tests pagerank.matrixMul()
    }
}

in cmd cd into src folder and do:

 javac pagerank/*.java

 java pagerank.PageRank
 java pagerank.PageRankTester
Evilguy
  • 306
  • 2
  • 14