简体   繁体   English

在netbeans中创建可运行的jar文件时遇到问题吗?

[英]Trouble creating a runnable jar file in netbeans?

Ive been searching for a couple of days now, and so i decided to turn to the community as a last resort! 我已经搜索了几天,所以我决定作为最后手段转向社区! forgive me if this has been answered before, i have not found anything on it. 原谅我,如果以前已经回答过,我还没有发现任何东西。

For a challenge issued from my computer science teacher for homework, I created a program that finds the absolute path for a specific directory(The Desktop in this case). 为了解决我的计算机科学老师提出的关于作业的挑战,我创建了一个程序,该程序可以找到特定目录(在本例中为“桌面”)的绝对路径。 It then creates a file on the desktop with the absolute desktop path written inside of it and opens a JOptionPane to confirm it has found the directory. 然后,它在桌面上创建一个文件,并在其中写入绝对桌面路径,并打开JOptionPane确认已找到该目录。 I then proceeded to create a runnable jar file by going to "Run>Clean and build project, but when I go to dist and press "Open With Java", it does not run. I understand my code may be inefficient, or it might be "the wrong way to do this", but I would like to find the solution to this problem. I do not know whether my code is necessary, so Ill add it in below: 然后,我进入“运行>清理并构建项目”,继续创建可运行的jar文件,但是当我转到dist并按“用Java打开”时,它无法运行。我知道我的代码效率低下,或者可能是“执行此操作的错误方法”,但我想找到解决此问题的方法。我不知道我的代码是否必要,因此请在下面添加它:

package FileExplorerandFinder;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import javax.swing.JOptionPane;

/**
 *
 * @author johnson.william2
 */
public class idk {

static PrintWriter p;
public static boolean onoff = true;

public static void displayFiles(File files[]) throws FileNotFoundException {
    if (files == null) {
        return;
    }
    for (int i = 0; i < files.length; i++) {
        if (onoff == true) {
            if (files[i].isDirectory()) {
                if (files[i].getAbsolutePath().contains("c:\\Users\\") && files[i].getAbsolutePath().contains("Desk") && !files[i].getAbsolutePath().contains("All Users") && !files[i].getAbsolutePath().contains("Default") && !files[i].getAbsolutePath().contains("Public") && !files[i].getAbsolutePath().contains("AppData")) {
                    onoff = false;
                    int a = i;
                    p = new PrintWriter(new File(files[i].getAbsolutePath() + "\\FileIndex.txt"));
                    p.println(files[i].getAbsolutePath());
                    p.close();
                    JOptionPane.showMessageDialog(null, files[i].getAbsolutePath());
                }
                displayFiles(files[i].listFiles());
            }
        }
    }
}

public static void main(String[] args) throws Exception {
    p = new PrintWriter(new File("c:FileIndex.txt"));
    File afile = new File("c:\\");
    File info[] = afile.listFiles();
    displayFiles(info);
}
}

UPDATE: I figured out the problem searching through the stack overflow questions a couple more hours. 更新:我发现搜索堆栈溢出问题的问题要花费几个小时。 The fix ended up being: (Right click the project, Properties, Run, Main Class) the main class was incorrectly selected for some odd reason, no reason as to why it was this way, but I am happy it is fixed. 该修复程序的最终结果是:(右键单击该项目,Properties,Run,Main Class)主类由于某种奇怪的原因而被错误地选择,没有理由这样做,但是我很高兴它已得到修复。 Thanks for the responses guys! 谢谢你们的回应! Much appreciated. 非常感激。

Follow the link here How to run Java Swing Application outside IDE to add the swing library and build the project. 单击此处的链接如何在IDE外部运行Java Swing应用程序以添加swing库并构建项目。

Then using the command line : 然后使用命令行:

java -jar "your jar location E.g. C:\Project\dist\Application.jar"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM