简体   繁体   English

如何解决Java错误“系统找不到指定的路径”?

[英]How to Solve Java Error “The system cannot find the path specified”?

I'm running Mac OS X and the code works. 我正在运行Mac OS X,并且代码有效。 My teammate is running Windows 7 and he gets the error (even though the directory structure and file exists on his system). 我的队友正在运行Windows 7,但他得到了错误(即使系统中存在目录结构和文件)。 Any ideas? 有任何想法吗? I have spent a number of hours on this and can't figure it out. 我已经花了几个小时来解决这个问题。 Thank you. 谢谢。

import java.io.BufferedReader;
import java.io.FileReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement; // Not used
import java.sql.ResultSet; // Not used
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList; // Not used
import java.util.Scanner; // Not used

public class CreateDB {

   public CreateDB() {
   }

   public void createDB() {

      Connection conn = null;

      String url = "jdbc:mysql://localhost:3306/";
      String dbName = ""; // Not used (yet?)
      String driver = "com.mysql.jdbc.Driver";
      String userName = "root"; // Tell user userName is "root". OR/TODO: Change to something more secure?
      String password = ""; // Tell user password is "". Or/TODO: Change to something more secure?
      String line;
      String sqlDump = null;
      String [] sqlDumpArray;

      String fileSeperator = System.getProperty("file.separator");
      System.out.println("fileSeperator is: " + fileSeperator);  // Testing
      String path = System.getProperty("user.dir");
      System.out.println("path is: " + path); // Testing
      String pathToFile = (path + fileSeperator + "src" + fileSeperator + "files" + fileSeperator);
      System.out.println("pathToFile is: " + pathToFile); // Testing.
      String fileName = "create_db_and_tables.sql"; // NOTE: Rename file, if necessary. Make sure it's in right place.
      System.out.println("pathToFile + fileName is: " + pathToFile + fileName); // Testing.

      try {
         Class.forName(driver).newInstance();
         conn = DriverManager.getConnection(url, userName, password);
         System.out.println("Connected to the database.\n"); // Testing

         BufferedReader reader = new BufferedReader(new FileReader(pathToFile + fileName));
         line = reader.readLine();
         while (line != null) {
            sqlDump += line + "\n";
            line = reader.readLine();
         }

         System.out.println(sqlDump); // Testing
         sqlDumpArray = sqlDump.split(";");
         System.out.println("sqlDumpArray size is " + sqlDumpArray.length); // Testing

         for (int i = 0; i < sqlDumpArray.length; i++) {
            System.out.println("\nsqlDumpArray[" + i + "] is: " + sqlDumpArray[i]);  // Testing
         }

         for (int j = 1; j < sqlDumpArray.length -1; j++) {
            try {
               Statement statement = conn.createStatement();
               statement.addBatch(sqlDumpArray[j]);
               statement.executeBatch(); // Could refactor or us another method
            }
            catch (SQLException e) {
               System.err.println("SQLException: " + e.getMessage());

            }
            catch (Exception e) {
               System.err.println("Exception: " + e.getMessage());
            }
         }
      }
      catch (java.lang.ClassNotFoundException e) {
         System.err.println("ClassNotFoundException: " + e.getMessage());

      }
      catch (SQLException e) {
         System.err.println("SQLException: " + e.getMessage());

      }
      catch (Exception e) {
         System.err.println("Exception: " + e.getMessage());
      }
      finally {
         try {
            // conn.commit(); // Not necessary
            conn.close();
            System.out.println("Successfully disconnected from database. Yeah!");
         }
         catch (SQLException e) {
            System.err.println("SQLException in 'finally' block: " + e.getMessage());
         }
      }
   }
   public static void main(String[] args) {
     CreateDB create = new CreateDB();
     create.createDB();
   }
}

Users on Windows have received the error when when trying to execute the database creation process. 尝试执行数据库创建过程时,Windows上的用户已收到错误。 This error occurs even when the path and file appear to be correct, such as what is returned by the following System.out.println statement in the file (highlighted below): 即使路径和文件看起来正确,例如文件中以下System.out.println语句返回的内容(以下突出显示),也会发生此错误:

String fileSeperator = System.getProperty("file.separator");
System.out.println("fileSeperator is: " + fileSeperator);
String path = System.getProperty("user.dir");
System.out.println("path is: " + path);
String pathToFile = (path + fileSeperator + "src" + fileSeperator + "files" + fileSeperator);
System.out.println("pathToFile is: " + pathToFile);
String fileName = "create_db_and_tables.sql";
System.out.println("pathToFile + fileName is: " + pathToFile + fileName);

This is an example of what that statement returns on a Windows 7 computer: 这是该语句在Windows 7计算机上返回的示例:

Exception: C:\Users\USER\workspace\mrbs2011a\src\files\create_db_and_tables.sql (The system cannot find the path specified)

That gets printed out even though USER has the file create_db_and_tables.sql located in the src/files folder. 即使USER在src/files夹中有文件create_db_and_tables.sql ,该文件也会被打印出来。

It's possible that the file is locked or that the user privileges under which the Java program is running don't allow access to that file. 该文件可能被锁定,或者Java程序在其下运行的用户特权不允许访问该文件。

If the file is actually there, then the file being locked (eg being held open for writing by another process) is probably the reason. 如果文件实际上在那,那么文件被锁定(例如,保持打开状态以供其他进程写入)可能是原因。 The documentation for FileReader says that it will throw FileNotFoundException if "the named file does not exist, is a directory rather than a regular file, or for some other reason cannot be opened for reading." FileReader文档说,如果“指定的文件不存在,不是目录而是常规文件,或者由于其他原因而无法打开进行读取,则将抛出FileNotFoundException ”。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java sftpChannel put错误(系统找不到指定的路径) - Java sftpChannel put error (The system cannot find the path specified) 系统找不到Java中指定的路径 - The system cannot find the path specified in java 系统找不到指定的路径(java eclipse) - The system cannot find the path specified (java eclipse) 如何解决系统在 Windows 10 Java 中找不到指定路径的错误? - How to resolve system cannot find the path specified error in windows 10 Java? 即使指定了 Path 变量,如何解决“CreateProcess error=2,系统找不到指定的文件”(跨平台) - How to fix “CreateProcess error=2, The system cannot find the file specified” even if the Path variable is specified (crossplatform) 文件错误为:-java.io.FileNotFoundException:\\ files \\ storetime.txt(系统找不到指定的路径) - Error in file is :- java.io.FileNotFoundException: \files\storetime.txt (The system cannot find the path specified) PrintWriter : java.io.FileNotFoundException: 系统找不到指定的路径 - PrintWriter : java.io.FileNotFoundException: The system cannot find the path specified java.io.IOException:系统找不到指定的路径 - java.io.IOException: The system cannot find the path specified 系统找不到指定的路径(Java开发工具包) - The system cannot find the path specified (Java Development Kit) java.io.IOException:系统找不到指定的路径 - java.io.IOException: The system cannot find the path specified
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM