Java错误:无法找到主类

时间:2014-12-05 07:10:08

标签: java linux bash classpath cplex

我正在尝试使用CLASSPATH&编译并运行一个java程序带有命令的PATH变量如下:

javac -classpath path_to_external_jar:. test.java

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

test.java代码

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);
}
}

test.java位于当前文件夹中。编译很顺利但是当我运行上面的第二个命令时,错误:Could not find the main class:test. Program will exit.跳出来。

有人对此问题有任何建议吗?

2 个答案:

答案 0 :(得分:1)

问题在于您的主要课程所在的package。Java机器无法在default package中完善您的课程。

从类中删除package cplextest;行或从put compiled class删除cplextest子文件夹并使用命令

运行
java -classpath path_to_external_jar:. cplextest.test

答案 1 :(得分:0)

如果您从根目录运行java程序,则需要指定完全分类的名称。

因此您需要将其作为

运行
java cplextest.test