Was ich tue:
Ich habe ein Stück Code, in dem ich den ausführbaren Pfad bekomme und es auf eine Beschriftung auf Schaltfläche klicken, um den erfolgreichen Jar-Pfad erfolgreich zu erhalten zur Laufzeit.Pfad von exportierten Glas funktioniert nicht
Problem:
Wenn ich durch Eclipse-Debug oder run-Modus gehe, bekomme ich den Pfad in der Beschriftung, aber wenn ich den Code in eine JAR-Datei exportieren, passiert nichts auf Knopfdruck.
Code:
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.io.File;
import java.net.URISyntaxException;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class TestProject extends JFrame {
private JPanel contentPane;
static JLabel lblNewLabel;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
TestProject frame = new TestProject();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public TestProject() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setLayout(new BorderLayout());
setContentPane(contentPane);
lblNewLabel = new JLabel("New label");
contentPane.add(lblNewLabel, BorderLayout.CENTER);
JButton btnNewButton = new JButton("New button");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
getJarFileRunningPath();
}
});
contentPane.add(btnNewButton, BorderLayout.SOUTH);
}
public static String getJarFileRunningPath()
{
String currentPath = null;
try
{
File currentFilePath = new File(TestProject.class.getProtectionDomain().
getCodeSource().getLocation().toURI().getPath());
currentPath = currentFilePath.getAbsolutePath();
lblNewLabel.setText(currentPath);
}
catch (URISyntaxException e)
{
}
return currentPath;
}
}
Update:
Ich fand irgendwie heraus, dass "Datei currentFilePath = new File ..." Zeile steckt immer, nicht Ausnahme werfen auch.
Danke in der Zukunft, für jede Hilfe. :-)
Haben Sie versucht, per Remote-Debugging zu sehen, was tatsächlich passiert? –
Hallo Jim, tut mir leid, ich weiß nicht, Remote-Debugging. :-( – Aman