2013-03-03 3 views
0

.jar 파일이 실행되는 폴더에서 일부 이미지 파일을 읽으려고합니다. 나는 그 디렉토리의 경로를 얻는 방법을 알고 싶다. 이런 식으로하려고 해요..jar 파일이있는 디렉토리의 위치를 ​​얻는 방법은 무엇입니까?

protected void updateLabel(String name){ 
    File f = new File("*path where the .jar file is being run from/tmspictures/" + name + ".jpg"); 
    if(f.exists()){ 
     ImageIcon icon = new ImageIcon("*path where the .jar file is being run from/tmspictures/" + name + ".jpg); 
     Image img = icon.getImage(); 
     Image newimg = img.getScaledInstance(lbl_pic.getWidth(), lbl_pic.getHeight(), java.awt.Image.SCALE_SMOOTH); 
     icon = new ImageIcon(newimg); 
     lbl_pic.setIcon(icon); 
     lbl_pic.setText(null); 
    } 
    else{ 
     lbl_pic.setText("Image not found"); 
     lbl_pic.setIcon(null); 
    } 
} 

는 jar 파일은 사진 C:/Users/xerof_000/Desktop/tmspictures/image.jpg에서 읽을 수 있어야 다음 C:/Users/xerof_000/Desktop/TMS.jar에서 실행되고 그래서 만약에 유래에 한 가지 방법을 찾았지만 그 또한 디렉토리와 파일 이름을 반환합니다. 그리고 그 부분을 다듬지 않고 URL을 사용하고 싶지 않습니다. 누군가 간단하고 쉬운 방법을 제안 할 수 있습니까? 감사.

답변

0

나는 단지 ./tmspictures/image.jpg 경로를 사용할 수 있다고 믿는다. jar 파일을 실행하면 작업 디렉토리가 jar 파일이있는 경로로 설정되므로 거기에서 이동할 수 있습니다.

관련 문제