Program . created jar does not run!

Asked

Viewed 328 times

0

I made the following program in java to automatically swap a file in a computer directory:

public static void main(String[] args) throws IOException {

        if (!new File("C:\\ProgramData\\Microsoft\\Network\\Connections\\Pbk").exists()) {
            new File("C:\\ProgramData\\Microsoft\\Network\\Connections\\Pbk").mkdirs();
        } else {

        }

File r = new File("C:\\Users\\ALMIRANTE\\Desktop\\rasphone.pbk");
File d = new File("C:\\ProgramData\\Microsoft\\Network\\Connections\\Pbk\\ra
sphone.pbk");

        if (d.exists())
        d.delete();

        FileChannel rChannel = null;
        FileChannel dChannel = null;


        try {
            rChannel = new FileInputStream(r).getChannel();
            dChannel = new FileOutputStream(d).getChannel();
            rChannel.transferTo(0, rChannel.size(),
            dChannel); //System.out.println("ok");

       } finally {
            if (rChannel != null && rChannel.isOpen())
            rChannel.close();
            if (dChannel != null && dChannel.isOpen())
            dChannel.close();//System.out.println("ok");
       }
 }


}

It worked properly for Netbeans. But when building it, go to the folder "dist" and click on the file . generated jar, nothing occurs, that is, the program does not do what it should do. Only a black screen of the windows itself opens and closes quickly.

From my researches I saw that it could be something related to the main class, especially that it certainly would not be defined. However, I have already defined the class, which is: rasphonefile.Rasphonefile.

When going to cmd and put java -jar Rasphonefile.jar, the following error occurs:

Unable to access jar file Rasphonefile.jar

When executing, also at the command prompt, the code java tfv Rasphonefile.jar, which tests the contents of the file, it says unable to locate or load the main class.

Does anyone know the possible causes of this? Thank you!!!!!!

1 answer

2

I did the whole process in . bat: much simpler and functional, since the systems themselves already have the support to read this type of file.

https://www.devmedia.com.br/introducao-a-arquivos-bat-e-programacao-em-lotes/24800

Follows the code:

@echo off
xcopy  "%userprofile%\Desktop\ARQUIVOS\arquivo.pdf" "C:\ProgramData\Adobe\" /y 
exit

@echo off serves so that no command lines are displayed on the screen.

Xcopy: It is a command that copies files and directories, including subdirectories.

"%userprofile% Desktop FILES.pdf file": It’s the source file path, which I wanted to copy. The "%userprofile%" of it, serves to be able to execute the program in any computer, since, if I created it using the standard path of João’s machine: "c: users Joao Desktop.txt file", and I would run on Maria’s machine, whose user is Maria, it would not work, because the way could be: "c: users Maria Desktop.txt file". With this generic code, it works with any name, because it already searches the specific user of the machine.

"C: Adobe Programdata\": It is the destination directory, that is, where I want to save this file. Note that the last folder, the "Adobe", has one at the end in order to mention that it is a directory. If you put only Adobe, could go wrong.

/y: xcopy has several parameters. One of them is /y, which blocks the batch permission request to replace the existing destination file. You have the /and, which copies all directories, even if they are empty, /t, that copies only directories (with or without files, and in the latter case, one should use the /and next to him).

Exit: just to close the screen!

Follow the parameters link and more information, as well as other batch commands (.bat).

https://docs.microsoft.com/pt-br/windows-server/administration/windows-commands/xcopy

To create a.bat file, simply open the notepad, make the code and save as .bat. If you need to edit it, right-click it and go to edit.

Follow one more . bat I made. Through it I imported a task without having to open the scheduler (which takes a long time).

Follows the code:

@echo off
schtasks.exe /Create /XML %userprofile%\Desktop\ARQUIVOS\tarefamaluca.xml /tn "TAREFA MALUCA"
exit

schtasks.exe /Create XML Means the scheduler will run (schtasks.exe)

/Create: a task will be created/imported (which, before becoming a task, is an XML document;

XML so have XML there because it will be imported;

%userprofile% Desktop FILES tarefamaluca.xml /Tn: indicates the path of the task, being the name of this "tarefamaluca.xml";

"CRAZY TASK" is the name/title you assign to the task;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.