hello, i am writing a program which will be turned into an executable .jar file. i need to locate the directory in which the jar file is located, using code within the jar file. my code works only in the directory where my source code is located (not the .class files, the .java files).
public class Example{ public static void main(String[] args){ try{ String exampleClassDirectory = Example.class.getResource("Example.class").getPath(); File jarFileDirectory = new File(exampleClassDirectory).getParentFile(); String containingDirectory = jarFileDirectory.getParent(); JOptionPane.showMessageDialog(new JFrame(), containingDirectory,"Found jar file directory.",JOptionPane.PLAIN_MESSAGE); } catch(Exception e){ JOptionPane.showMessageDialog(new JFrame(), e.toString(),"error",JOptionPane.ERROR_MESSAGE); } } }