Sylvia
Sylvia

Reputation: 139

Java Error: Could not find the main class

I am trying to compile and run a java program with CLASSPATH & PATH variables with commands as follows:

javac -classpath path_to_external_jar:. test.java

java -Djava.library.path=external_lib_folder -classpath path_to_external_jar:. test

test.java code:

package cplextest;

import ilog.concert.*;
import ilog.cplex.*;

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


//--------------------------------------------------------------------------
//File: examples/src/QPex1.java
//Version 9.0    
//--------------------------------------------------------------------------
//Copyright (C) 2001-2003 by ILOG.
//All Rights Reserved.
//Permission is expressly granted to use this example in the
//course of developing applications that use ILOG products.
//--------------------------------------------------------------------------
//
// Entering and optimizing a QP problem



public class test {
public static void main(String[] args) {
   try {
      IloCplex cplex = new IloCplex();
      IloLPMatrix lp = populateByRow(cplex);

//#ifdef FULLTEST
//      cplex.setParam(IloCplex.IntParam.Threads,
//                     cplex.getMax(IloCplex.IntParam.Threads));
//#endif

      if ( cplex.solve() ) {
         double[] x     = cplex.getValues(lp);
         double[] dj    = cplex.getReducedCosts(lp);
         double[] pi    = cplex.getDuals(lp);
         double[] slack = cplex.getSlacks(lp);

         System.out.println("Solution status = " + cplex.getStatus());
         System.out.println("Solution value  = " + cplex.getObjValue());

         int ncols = lp.getNcols();
         for (int j = 0; j < ncols; ++j) {
            System.out.println("Column: " + j +
                               " Value = " + x[j] +
                               " Reduced cost = " + dj[j]);
         }

         int nrows = lp.getNrows();
         for (int i = 0; i < nrows; ++i) {
            System.out.println("Row   : " + i +
                               " Slack = " + slack[i] +
                               " Pi = " + pi[i]);
         }

         cplex.exportModel("qpex1.lp");
      }
      cplex.end();
   }
   catch (IloException e) {
      System.err.println("Concert exception '" + e + "' caught");
   }
}

static IloLPMatrix populateByRow(IloMPModeler model) throws IloException {
   IloLPMatrix lp = model.addLPMatrix();

   double[]    lb = {0.0, 0.0, 0.0};
   double[]    ub = {40.0, Double.MAX_VALUE, Double.MAX_VALUE};
   IloNumVar[] x  = model.numVarArray(model.columnArray(lp, 3), lb, ub);

   double[]   lhs = {-Double.MAX_VALUE, -Double.MAX_VALUE};
   double[]   rhs = {20.0, 30.0};
   double[][] val = { {-1.0,  1.0,  1.0},
                      { 1.0, -3.0,  1.0} };
   int[][]    ind = { {0, 1, 2},
                      {0, 1, 2} };
   lp.addRows(lhs, rhs, ind, val);

   // Q = 0.5 ( 33*x0*x0 + 22*x1*x1 + 11*x2*x2 - 12*x0*x1 - 23*x1*x2 )
   IloNumExpr x00 = model.prod( 33, x[0], x[0]);
   IloNumExpr x11 = model.prod( 22, x[1], x[1]);
   IloNumExpr x22 = model.prod( 11, x[2], x[2]);
   IloNumExpr x01 = model.prod(-12, x[0], x[1]);
   IloNumExpr x12 = model.prod(-23, x[1], x[2]);
   IloNumExpr Q   = model.prod(0.5, model.sum(x00, x11, x22, x01, x12));

   double[] objvals = {1.0, 2.0, 3.0};
   model.add(model.maximize(model.diff(model.scalProd(x, objvals), Q)));

   return (lp);
}
}

The test.java is in the current folder. The compilation went through fine but when I run the second command above, the error:Could not find the main class:test. Program will exit. jumps out.

Anyone has advice on this issue?

Upvotes: 0

Views: 1014

Answers (2)

eatSleepCode
eatSleepCode

Reputation: 4637

If your running java program from root directory then you need to specify fully classified name.

so you need to run it as

java cplextest.test

Upvotes: 0

The problem is with the package your main class is in. Java machine cannot fine your class in default package.

Either remove package cplextest; line from the class or put compiled class into cplextest subfolder and run with command

java -classpath path_to_external_jar:. cplextest.test

Upvotes: 1

Related Questions