-10

How can I create a file in the same folder as that of a .jar file, which is located inside the main project directory?

Shankha057
  • 1,296
  • 1
  • 20
  • 38
chicoferreira
  • 13
  • 1
  • 6
  • 3
    What have you tried so far? Please post the code. Please provide more details. – Shankha057 Jan 06 '18 at 23:19
  • If you are using an IDE (netbeans, eclipse, idea) they should be automatically included when you export to jar. What issue are you having? – BackSlash Jan 06 '18 at 23:19
  • 1
    Please read the [how to ask a good question guide](https://stackoverflow.com/help/how-to-ask) before asking another question. At the moment, your question does not include enough details for someone to answer and fix your issue. – Micheal O'Dwyer Jan 06 '18 at 23:26
  • I have a .data custom file on my eclipse project folder, and I want when the .jar opens, "paste" that custom file that is on the project folder on where .jar was open. – chicoferreira Jan 06 '18 at 23:33
  • What do you mean by ".jar opens"? Also, what do you mean by "paste"? Do you want to "copy and paste" or do you want to "move" the custom file?Also, have you tried anything on your own? – Shankha057 Jan 06 '18 at 23:42
  • ".jar opens" when the .jar is executed, "paste" is to copy the file that which is inside the project and paste it into the folder where .jar has been executed. And I tried to do this many times – chicoferreira Jan 06 '18 at 23:48
  • Is your jar file executable? – Shankha057 Jan 07 '18 at 00:40
  • Yes it is. It's a minecraft plugin to be honest. – chicoferreira Jan 07 '18 at 01:09

1 Answers1

0

You have to get the name of the class where the main function resides(the Minecraft plugin's main function's class name). I will indicate this as MSMainClass Now, after you have the name of the class check whether the loaded class is indeed from a jar file or not. And this is how you do it:

String className = MSMainClass.class.getName().replace('.','/');
String classJar = MSMainClass.class.getResource("/" + className + ".class").toString();
if(classJar.startsWith("jar:") //code to copy files to destination;

Here is some reference for file copying in Java.
Here is some reference for jar execution detection(possible duplicate).

Shankha057
  • 1,296
  • 1
  • 20
  • 38
  • I don't know about Eclipse(used it once and didn't go back to using after IntelliJ). But in IntelliJ IDEA, you just right click the file and copy the path,then follow the link that I have sent you. There are 4 ways in which you can do it and also light explanations about each way. So, good luck. – Shankha057 Jan 08 '18 at 01:32