在同一目录中执行代码时出错

时间:2017-03-06 11:38:56

标签: java

我有这个java文件

ATestClass.java         DataStore.java      RequestsManager.java
ConnectionManager.java  ImportKeyPair.java  SSLClientTest.java

这就是他们在终端中看待的方式

root@ubuntu-2gb-nyc1-01:~/pdsl/src/pdslipay# ls
ATestClass.java         DataStore.java      RequestsManager.java
ConnectionManager.java  ImportKeyPair.java  SSLClientTest.java
root@ubuntu-2gb-nyc1-01:~/pdsl/src/pdslipay# javac ATestClass.java
ATestClass.java:10: error: cannot find symbol
                 RequestsManager req = new RequestsManager();
                 ^
  symbol:   class RequestsManager
  location: class ATestClass
ATestClass.java:10: error: cannot find symbol
                 RequestsManager req = new RequestsManager();
                                           ^
  symbol:   class RequestsManager
  location: class ATestClass
2 errors
root@ubuntu-2gb-nyc1-01:~/pdsl/src/pdslipay#

我正在尝试运行AtestClass.java但是我收到了错误

ATestClass.java:10: error: cannot find symbol
                 RequestsManager req = new RequestsManager();
                 ^
  symbol:   class RequestsManager
  location: class ATestClass
ATestClass.java:10: error: cannot find symbol
                 RequestsManager req = new RequestsManager();
                                           ^
  symbol:   class RequestsManager
  location: class ATestClass
2 errors

我将文件RequestsManager.java放在与ATestClass.java相同的目录中

ATestClass.java看起来像这样

package pdslipay;

public class ATestClass {

     public static void main(String[] args) {

         RequestsManager req = new RequestsManager();

         try {
            req.payBillPrePaid("logan", "0", "saf", "000581");
        } catch (Exception e) {
            e.printStackTrace();
        }

     }
}

和RequestsManager.java看起来像这样

package pdslipay;

import java.io.StringWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class RequestsManager {
....
}

如果两个文件位于同一目录中,我无法解释错误的来源。

1 个答案:

答案 0 :(得分:0)

您需要编译文件:

root@ubuntu-2gb-nyc1-01:~/pdsl/src/pdslipay# javac *.java

然后切换到项目的根目录,即包结构启动的目录,并在运行Class时使用包名称:

root@ubuntu-2gb-nyc1-01:~/pdsl/src/pdslipay# cd ..
root@ubuntu-2gb-nyc1-01:~/pdsl/src# java pdslipay.ATestClass

(假设您没有使用IDE或其他工具将文件编译到另一个目录结构中,如~/pdsl/bin/

编译后,目录“pdslipay”中应该有* .class文件。必须为编译器javac提供要编译的* .java文件。虚拟机java请求包含main方法(“pdslipay.ATestRequest”)的完全限定类名,而不是* .class文件。虚拟机将搜索CLASSPATH环境中列出的目录中的类,或者由-cp选项提供的类(默认为“。”,实际目录)。