1

I am not talking about android development,i want to know if i could know if a java program is run for the first time.My program is supposed to get a user input for a password and check if the password matches the stored encrypted password and then fill some arrays and variables.But if it is the first time running the program then the user is supposed to create a password and then create the .txt and write the data.Apart from creating the .txt first is there a way to do this?

    public class MyClass {

    AccountReader reader = new AccountReader();
    AccountWriter writer = new AccountWriter();

    public static void main(String args[]) {


        if(its the first time launching) do something
        else{
            reader.readFromFile();
            do something else
        } 
        .
        .
        .
        writer.writeToFile();

    }
}
John Joe
  • 12,412
  • 16
  • 70
  • 135
Petros21
  • 45
  • 7
  • 2
    You need to store the information somewhere, for example as a file or in a database. – assylias Aug 24 '18 at 09:31
  • 1
    You can use a file and store in that, if thats the first launch – Donatic Aug 24 '18 at 09:31
  • You would need to store that somewhere on the machine. For example in a file at a specific location or in the preferences. – Henry Aug 24 '18 at 09:31
  • 1
    What situation do you regard as *first run*? You could just check for an existing .txt file and assume the code is being run for the first time if there is no .txt file. If the file gets deleted manually or by another program, then your program would regard the next run as a *first* one and ask for a registration again. – deHaar Aug 24 '18 at 09:31
  • 2
    Guys, read the post: he said *Apart from creating the .txt first is there a way to do this?* – Leviand Aug 24 '18 at 09:31
  • @Leviand the way I understand it, OP wants to store the passwords and other data in that file. So verifying the presence of an empty file to determine if the program was already executed seems a valid option, no? – GameDroids Aug 24 '18 at 09:38
  • What I'm guessing from his post is: I've actually achieved that by reading the presence of the txt file, but I want to know if there's a different / better solution. – Leviand Aug 24 '18 at 09:41
  • 1
    @Leviand you are right, I missread it. Petros21: The answer from Donatic is correct, you have to "remember" it somewhere. So in any way you would be accessing the filesystem. Even if you where to develop for Windows only and wanted to use the registry. – GameDroids Aug 24 '18 at 09:49
  • Is this useful? https://docs.oracle.com/javase/8/docs/technotes/guides/preferences/overview.html – vikingsteve Aug 24 '18 at 09:50

3 Answers3

2

You cant "remember" that in the programm, but you can save it in a file. Thats because all variables in your programm "resets", when your programm is terminated.

So you can write that into a file: How do I create a file and write to it in Java?

or create a file on the first launch and look every start, if that file already exists: How do I check if a file exists in Java?

Donatic
  • 323
  • 1
  • 13
  • ok the second link was helpful, I found this answer File f = new File(filePathString); if(f.exists() && !f.isDirectory()) { // do something } How does this work exactly?Do i need to set the variable filePathString myself,or it is the same as the place the java program is stored?Shouldn't i give as an input the name of the .txt? – Petros21 Aug 24 '18 at 10:05
1

Java programs run as a process in some container, for example JVM or a server like tomcat. Any process can be "killed" and restarted, thus your Java program will never be able to tell if it ran for the first time. From the java programs perspective, every time it is launched, it is the first time.

How we typically come around it is by persisting that information in a place outside the program. This is typically a database or a file in a non-temp folder.

You have to decide what constitutes a "first" run. Can the absence of your password file be considered as a first run? If so, you can use that check. Else, you can get inspiration from other applications, for example, bash or git etc, they will typically create a .rc file in the home directory. ~/.bashrc. You can treat it as your store. You can create your own .rc file and store your preferences there. Absence of that file can be treated as the "first run".

dubes
  • 5,324
  • 3
  • 34
  • 47
  • Thanks for your answer.When i say first run i mean exactly that, the first ever time the application is being launched.I know i can't do that through just code,so I was wondering how to do it without having to manually create the .txt file.I figured that i should check if the .txt file exists and if not, this would be the first run.I added an answer with code that you could check and verify that it works. – Petros21 Aug 24 '18 at 12:20
1

So thanks to @Donatic I figured out i need to checked if the wanted .txt exists and if not then this is the first time launching the program.If i want to make it somewhat flexible so that it works in every computer will something like that work?Sorry for not testing myself but i can only work in an online editor and i don't think i can check it from there.

URL main = Main.class.getResource("Main.class");
File javaFile = new File(main.getPath());

String absolutePath = javaFile.getAbsolutePath();
String javaFileFolderPath = absolutePath.substring(0,absolutePath.lastIndexOf(File.separator));
String txtFilePath = javaFileFolderPath+"\\wantedTxt.txt";

File txtFile = new File(txtFilePath);
if(txtFile.exists() && !txtFile.isDirectory()) { 
    // this is not the first time running the program
}
else{
    //this is the first time running the program
}
Petros21
  • 45
  • 7