0

Problem: Two classes try to access two different (relative) paths using the same piece of code.

Steps:

  1. I run Client.java
  2. Client prints C:\Users\user\git\x\y as relative path (The correct path)
  3. The program reaches getSchemaStatements() from SQLiteDB.java
  4. SQLiteDB now prints C:/WINDOWS/System32 while it was set as C:\Users\<user>\git\x\y in the Client

Class client.java:

public class Client {

@SuppressWarnings("unused")
private static TUI tui;
public static final String RELATIVE_PATH = Paths.get("").toAbsolutePath().toString();

public static void main(String[] args) {
    System.out.println(RELATIVE_PATH); 
    // Prints C:\Users\user\git\x\y
    // The above path is the correct path

    tui = new TUI();        
}

Class SQLiteDB.java

public class SQLiteDB {
public static final String RELATIVE_PATH = Paths.get("").toAbsolutePath().toString();

public static void initializeDB() {
    //<snip>
}

private static String getSchemaStatements() {
    List<String> lines = null;
    try {
        System.out.println(RELATIVE_PATH); // Prints C:/WINDOWS/System32
        System.out.println(Client.RELATIVE_PATH); // Now suddenly also prints C:/WINDOWS/System32 while it was set as C:\Users\user\git\x\y
        lines = Files.readAllLines(schema);
    } catch (IOException e) {
        e.printStackTrace();
    }
    //<snip>
}

Am I missing something here? I appreciate your help!

EDIT: Stacktrace:

C:\WINDOWS\System32\z.txt
at sun.nio.fs.WindowsException.translateToIOException(Unknown Source)
at sun.nio.fs.WindowsException.rethrowAsIOException(Unknown Source)
at sun.nio.fs.WindowsException.rethrowAsIOException(Unknown Source)
at sun.nio.fs.WindowsFileSystemProvider.newByteChannel(Unknown Source)
at java.nio.file.Files.newByteChannel(Unknown Source)
at java.nio.file.Files.newByteChannel(Unknown Source)
at java.nio.file.spi.FileSystemProvider.newInputStream(Unknown Source)
at java.nio.file.Files.newInputStream(Unknown Source)
at java.nio.file.Files.newBufferedReader(Unknown Source)
at java.nio.file.Files.readAllLines(Unknown Source)
at java.nio.file.Files.readAllLines(Unknown Source)
at database.SQLiteDB.getSchemaStatements(SQLiteDB.java:45)
at database.SQLiteDB.initializeTableStructure(SQLiteDB.java:61)
at database.SQLiteDB.initializeDB(SQLiteDB.java:30)
at database.DataManager.init(DataManager.java:34)
at database.DataManager.initIfRequired(DataManager.java:50)
at database.DataManager.getObjectsFromDB(DataManager.java:114)
at server.rest.ClientHandler.getAuthToken(ClientHandler.java:874)
at server.rest.ClientHandler.parseJSONRequest(ClientHandler.java:79)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory$1.invoke(ResourceMethodInvocationHandlerFactory.java:81)
at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher$1.run(AbstractJavaResourceMethodDispatcher.java:144)
at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:161)
at org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$ResponseOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:160)
at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:99)
at org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:389)
at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:347)
at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:102)
at org.glassfish.jersey.server.ServerRuntime$2.run(ServerRuntime.java:326)
at org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
at org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
at org.glassfish.jersey.internal.Errors.process(Errors.java:267)
at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:317)
at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:305)
at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1154)
at org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:473)
at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:427)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:388)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:341)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:228)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:478)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:80)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:624)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:799)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:861)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1455)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)
JW P
  • 153
  • 11
  • Where was `CLIENT` defined? You have a class `Client` note that case sensitive is a major thing in Java! – Jorge Campos Jun 10 '17 at 13:06
  • @JorgeCampos Oops, that should've been Client (Copied wrongfully) – JW P Jun 10 '17 at 13:09
  • Ok, then, how/where are you calling the SQLiteDB class? – Jorge Campos Jun 10 '17 at 13:11
  • 2
    Possible duplicate of [Getting the Current Working Directory in Java](https://stackoverflow.com/questions/4871051/getting-the-current-working-directory-in-java) – Luciano van der Veekens Jun 10 '17 at 13:13
  • @LucianovanderVeekens `System.getProperty("user.dir")` also gives me `C:\WINDOWS\System32` – JW P Jun 10 '17 at 13:19
  • @JorgeCampos I added the stacktrace to the post. There's a bunch of code involved, but none of which I can see that could cause a relative path change.. – JW P Jun 10 '17 at 13:20
  • 1
    You didn't answer what I asked, "how/where are you calling the SQLiteDB class?" To know how your project setup is and how the classes are connected and how are you calling then will certainly help us to help you. By the stacktrace I see that you are running inside a container (tomcat) which you didn't mentioned, and you Client class has a main method, so explain how is your project's set up – Jorge Campos Jun 10 '17 at 13:26

0 Answers0