0

I am trying to access a file like this:

public class Main {

 private static class RemoteAdapter implements SpellcheckerRemoteAdapter {

  private Spellchecker spellchecker;
  private String fileName = "deutsch.txt";
  File file = new File(getClass().getClassLoader().getResource(fileName).getFile());

  private RemoteAdapter() {
   this.spellchecker = SpellcheckerFactory.createSpellchecker(file);
  }

  @Override
  public boolean check(String word) {
   return this.spellchecker.check(word);
  }

  @Override
  public List < String > getProposal(String word) {
   return this.spellchecker.getProposal(word);
  }

 }

 public static void main(String[] args) {

  System.out.println("Start Server");
  try {
   Registry registry = LocateRegistry.createRegistry(1088);

   SpellcheckerRemoteAdapter adapter = new RemoteAdapter();
   UnicastRemoteObject.exportObject(adapter, 0);
   registry.rebind(SpellcheckerRemoteAdapter.NAME, adapter);
  } catch (RemoteException re) {
   System.out.println(re.getMessage());
  }

  System.out.println("Server is running!");
 }
}

When I run my code inside of eclipse it works just fine. But if I export the project as a runnable jar I get the following error:

java.io.FileNotFoundException: file:\C:\Users\Linsane\Desktop\server\SpellcheckerServer.jar!\deutsch.txt (The filename, directory name, or volume label syntax is incorrect)

Although I know that the file is in this location and as I don't build the path myself I don't really get what went wrong.

Linsane
  • 338
  • 4
  • 16
  • Resources are not files. You have the URL: use that to get an input stream directly. – user207421 May 21 '18 at 23:00
  • @EJP Would you kindly explain a bit more what you mean exactly? I don't really get it. Sorry – Linsane May 21 '18 at 23:12
  • Err, `getClass().getClassLoader().getResource(fileName).openStream()`? or getClass().getClassLoader().getResourceAsStream(fileName)`? This is all documented. – user207421 May 22 '18 at 10:57

2 Answers2

-1

Try removing getClassLoader() call:

  File file = new File(getClass().getResource(fileName).getFile());

You can also open the jar file manually and check if the file is actually packaged into the jar file.

Amila
  • 5,195
  • 1
  • 27
  • 46
  • If remove the getClassLoader() I receive a NullPointerException. And yes the file is in fact inside of the jar, – Linsane May 21 '18 at 18:29
-1

I solved it with the tip of EJP & What's the difference between a Resource, URI, URL, Path and File in Java?

Instead of passing a File like this:

private String fileName = "deutsch.txt";
  File file = new File(getClass().getClassLoader().getResource(fileName).getFile());

  private RemoteAdapter() {
   this.spellchecker = SpellcheckerFactory.createSpellchecker(file);
  }

I now pass a URL to the file like this:

private String fileName = "deutsch.txt";
URL url = ClassLoader.getSystemClassLoader().getResource(fileName);
private RemoteAdapter() {
        this.spellchecker = SpellcheckerFactory.createSpellchecker(url);
}

Inside of the Spellchecker constructor I then just get an Inputstream of the URL and read from it.

Linsane
  • 338
  • 4
  • 16
  • 'Instead of' is not an answer. What you put instead is an answer, but you haven't provided it. – user207421 May 22 '18 at 10:58
  • I accidently hit "Post your answer" to early, as I hit tab and then blank, which actually tabs to the "Post your answer" - Button and then posts it. – Linsane May 22 '18 at 11:00