-2

Everytime I try to run my program it opens and then closes immediately in CMD. Here's the code if it helps. Also, yes I have JRE installed.

import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.Math;

class animangaTracker{
    public static void main(String[] args) {
        Scanner n = new Scanner(System.in);
            System.out.println("Enter the date, format: mm-dd-yy, eg. 09-23-19.");
        String x = n.nextLine();
            System.out.println("Enter the Anime/Manga/Book Title, eg. Durarara.");
        String y = n.nextLine();
            System.out.println("Enter the Episode/Chapter/Page, eg. Chapter 67.");
        String z = n.nextLine();
            System.out.println("Done!");
        File file = new File("output2.txt");
        try {
            FileWriter fr = new FileWriter(file, true);
            fr.write(x + "\n" + y + ", " + z + "\n");
            fr.close();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
    }
}
Seco01
  • 3
  • 4
  • You could try catching `Exception` instead of `IOException` – right now, if some non-IOException is thrown, your code won't catch (or log) it. Any other specifics about what happens would be helpful to share. – Kaan Sep 23 '19 at 22:07
  • Do you see the first System.out.println output or does it fail with no output? – Kevin Hooke Sep 23 '19 at 22:14
  • All you do is to change the code from IOException to Exception right, nothing else. If that's the case the same issue still occurs. – Seco01 Sep 23 '19 at 22:15
  • 1
    @kaan and what is the problem if an exception is not catch by code? – user85421 Sep 23 '19 at 22:16
  • @KevinHooke it still fails with no output. It just opens and closes immediately. Nothing else happens. – Seco01 Sep 23 '19 at 22:18
  • 1
    how exactly are you starting it? and compiling? – user85421 Sep 23 '19 at 22:19
  • @CarlosHeuberger I exported it as a runnable jar file. The program works perfectly fine in Eclipse, however, when I export it, it just opens and closes. Sorry if this was unclear. – Seco01 Sep 23 '19 at 22:22
  • (unclear? missing at all...) and my questions are still unanswered, worse more questions, how is the JAR being built (code is perfectly fine, at least runs and ends with `Done!`) – user85421 Sep 23 '19 at 22:24
  • 1
    if you run your executable jar with 'java -jar yourjarname.jar' what does it do? what OS are you running it on, and how are you running it, it sounds like you're double-clicking it and nothing it happening? it sounds like your OS is not recognizing an associated executable with opening the jar by double clicking it – Kevin Hooke Sep 23 '19 at 22:26
  • How do I run the executable jar with the stuff you wrote? Is it something in properties. I'm running it by double click it and I'm using Windows 10. @KevinHooke – Seco01 Sep 23 '19 at 22:34
  • 1
    maybe: https://stackoverflow.com/a/8646493/85421 or/and https://stackoverflow.com/a/2022372/85421 - run the checks (middle of answer) first – user85421 Sep 23 '19 at 22:49
  • from your cmd.exe command line, in the same dir where your .jar is: 'java -jar yourjarname.jar'. Rather than open and close quickly, it should at least output something to the console so we can get a better idea what it's doing. If there's an error, post the error message here – Kevin Hooke Sep 23 '19 at 23:19
  • @KevinHooke it says, Exception in thread "main" java.lang.UnsupportedClassVersionError: animangaTracker has been compiled by a more recent version of the Java Runtime (class file version 56.0), this version of the Java Runtime only recognizes class file versions up to 52.0 at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClass(Unknown Source) at java.security.SecureClassLoader.defineClass(Unknown Source) at java.net.URLClassLoader.defineClass(Unknown Source) at java.net.URLClassLoader.access$100(Unknown Source) – Seco01 Sep 23 '19 at 23:58
  • Ahah ok, so the JRE in your PATH on Windows is an older version than the version you compiles the source code with in Eclipse. Major version 52 in your Windows PATH is Java 8, and version 56 that you compiled the code with is Java 12. Either install a Java 12 JRE on Windows, or compile your code with Java 8 in Eclipse – Kevin Hooke Sep 24 '19 at 00:08
  • @KevinHooke I already have JDK 12.0.2. Is this fine? I can't find a download of Java 8 without having to make an account on Oracle's website. – Seco01 Sep 24 '19 at 00:23
  • If you can update your PATH to put the JDK12 first in your path, then it should work. If you do a 'java -version' from your cmd.exe and it shows Java 12 then you should be good, but if it still shows Java 8 then you need to fix this first – Kevin Hooke Sep 24 '19 at 00:39
  • Alternatively if having multiple Java versions installed is causing too many issues and you don't want to fiddle with the PATH, uninstall all the other JDK/JRE versions that you don't need so you only have a single JDK12 installed, that would work too – Kevin Hooke Sep 24 '19 at 00:41
  • There's a good walkthrough of setting the PATH environment variable in one of the answers here https://stackoverflow.com/questions/1672281/environment-variables-for-java-installation – Kevin Hooke Sep 24 '19 at 00:46
  • @KevinHooke I did java -version, and I ended up with it saying the correct stuff, however, my program still crashes upon double clicking it. – Seco01 Sep 24 '19 at 01:57
  • From the commandline does it show JDK12 is the current JDK in your PATH? If so and then you run your jar with 'java -jar' do you still get the same error or something else? – Kevin Hooke Sep 24 '19 at 02:11
  • Well, the command prompt works well now java -jar thing works but I still for some reason can't open the file mangaTracker.jar file itself. Still opens and closes immediately. Also when I ran the program through command prompt it didn't output to my output2.txt. – Seco01 Sep 24 '19 at 02:36

1 Answers1

0

Open a terminal/command prompt and run the following command:

java -jar {/path/to/yourJarName}.jar