The project doesn’t work in other computers

已回答

The resources of the project can’t be accessed by the system when I use different computers.I want to know how to change the absolute path .I have try some recommended methods but the login frame continuously shows that the user doesn’t exist.

0

Hello,

Could you please provide more details about the problem? 

Feel free to use screenshots or code snippets, and describe the expected vs. actual result.

1

Hi,

I have a uni project about renting a car and it needs to be working on different devices but my program works only in my working computer.It is a gui app and the program needs to recognise the provided csv files to work.When you run the main,the result is a login frame and you use one of the required users for the initialisation. I have tried finding the solution from different sources like not using absolute path but the problem is still the same .The structure of the project is CAR_RENT_project_despoina/
 .ideaout
 resources/
customers.csv
 users.csv
 vehicles_with_plates.csv
 src/                     
 api/
Car.java
 Customer.java
DataManager.java
Employee.java
Rental.java
 gui/        
  Main.java      
The code is in data management and it is:

package api;import java.util.*;import java.time.LocalDate;import java.io.*;import java.nio.charset.StandardCharsets;

public class DataManager {
 public HashMap<String, Employee> employees = new HashMap<>();
 public HashMap<String, Customer> customers = new HashMap<>();
 public HashMap<Integer, Car> cars = new HashMap<>();
   public HashMap<Integer, Rental> rentals = new HashMap<>();
   private int nextRentalId = 1;
   private final String vehiclesCSV = “C:/USERS/despo/Desktop/CAR RENT project_despoina/resources/vehicles_with_plates.csv”;private final String usersCSV = "C:/USERS…/resources/users.csv";

    private final String customersCSV = "C:/USERS…/resources/customers.csv";

     public DataManager(){

        loadEmployees();

        loadCars();

         loadCustomers();

    }


              

0

Hello Bariemaib 

As you mention, the absolute paths (like C:/Users/despo/...), which are hardcoded, address a specific path to your own computer's file system. When you move the project to another device, the system looks for that exact folder structure, which appears not to match the user.

So, in a Java project, relative paths are resolved starting from the Project Root (the CAR_RENT_project_despoina folder). You need to change your hardcoded strings to point to the resources folder relative to the project root. Here is a proposal on how to update your DataManager.java to make it portable:

// ... existing code ...
    public HashMap<Integer, Rental> rentals = new HashMap<>();
    private int nextRentalId = 1;

    // Use relative paths starting from the project root
    private final String vehiclesCSV = "resources/vehicles_with_plates.csv";
    private final String usersCSV = "resources/users.csv";
    private final String customersCSV = "resources/customers.csv";

    public DataManager(){
// ... existing code ...

It will work for anyone, as long as they have the resources folder inside the project folder. So, verify on the other computer that the user has enough privileges to access the directory.

If the login frame still says "user doesn't exist" after this change, check these two things:

  1. Working Directory: In IntelliJ, go to Run -> Edit Configurations.... Look at your main application configuration and ensure the Working directory is set to your project root (e.g., .../CAR_RENT_project_despoina).
  2. Case Sensitivity: On macOS or Linux, file names are case-sensitive. Ensure your code matches the filename exactly (e.g., resources vs Resources).

Happy holidays!

0

请先登录再写评论。