0

I am doing a project for school and everything was working fine until this morning. When i opened my code and attempted to run it via the command prompt it kept saying- Error: could not find or load main class bag.test.java My source code is here on github: https://github.com/Knightbomb8/CS-2400-Project1 To run the code I first compile the code inside the bag folder running: javac*.java I then back out of the bag folder so I am in the project folder. I then attempt to run the code doing java bag.test.java and am then met with the error. However, when I use vsCode to run my code I do not meet any issues. Does anyone know how to resolve this issue I am having as I am perplexed. Thank you

Doing some more testing, any file I try to run on my computer is giving me this error. So it seems it is not this project specific.

Paths

2 Answers2

0

it is probably because you are compiling the code in one place and the compiled byte code file is created there . And when . you change directory using the command prompt u are going out of the folder where the byte code file is , you will have to run the file in the same place as you created it from

  • So you are saying that if I compile my code inside the bag folder, that is where I need to run it from? I just tried that and it hits me with: Error: Could not find or load main class test.java Whenever I ran it yesterday from outside the folder it was working also. – Donald Turner Oct 06 '19 at 21:10
0

When you run java code from the command line you do not add the .java or .class suffix. You should just run java bag.test without the .java.

Jannik
  • 1,583
  • 1
  • 14
  • 22
  • okay sounds good. I just tried that and its telling me "bag/test has been compiled by a more recent version of the Java Runtime (class file version 57.0), this version of the Java Runtime only recognizes class file versions up to 52.0". Do you know which java runtime environment supports class file versions of atleast 57? Thank you – Donald Turner Oct 06 '19 at 21:59
  • You probably have multiple versions of java installed. To check which version you are running in the command line run `java -version`. 52 corresponds to Java 8 and 57 to Java 13 – Jannik Oct 06 '19 at 22:02
  • java version "1.8.0_221" Java(TM) SE Runtime Environment (build 1.8.0_221-b11) Java HotSpot(TM) 64-Bit Server VM (build 25.221-b11, mixed mode) This is what I get when i type version. Where can i see if i have two different versions installed? – Donald Turner Oct 06 '19 at 22:04
  • This depends on your operating system and how you installed Java. You can look if you have an environment variable called `JAVA_HOME`. This is a common place to put the path of the JDK which is probably used in VS Code. To use the newer version of Java you must update your `PATH` variable to include the newer Java version. – Jannik Oct 06 '19 at 22:12
  • I am using Windows. Currently my path leads to "C:\Program Files\Java\jdk-13\bin" which I have installed. when i go to %programfiles%/java all I see are jdk 13 and jre 1.8.0_221 As far as I know these are correct for class file version 57. I installed the jdk 13 using the .exe file oracle provides after I uninstalled jdk 12. – Donald Turner Oct 06 '19 at 22:14
  • The JDK includes a Java Runtime, so you probably only need the JDK. To use Java 13 you must update the PATH in `edit the system environment variables` > `Advanced` > `Environment Variables`. Make sure that there is only the newer Java version in the path and then restart your command window. – Jannik Oct 06 '19 at 22:18
  • Yeah i just double checked and I have already put it in the path. I attached a picture to the original post for visual proof. So I have jdk 13 which runs version 57 and then I have a version that is running 52 which is somehow being called. I just reopened the command prompt and tried to run it again but have the same error. – Donald Turner Oct 06 '19 at 22:27
  • I think you must delete the first line (`.../javapath`) in the path. I believe this points to Java 8 (just open the path and look where it goes). The operating system searches for the java exe in your path from the top to bottom, so Java 8 is ran instead of Java 13 – Jannik Oct 06 '19 at 22:31
  • Thank you so much for helping me go through that man. It is working correctly. A true lifesaver. Is there anyway to give someone upmarks on here? – Donald Turner Oct 06 '19 at 22:36