1

Update after the first feedback. Thank you so much. I did read What does “Could not find or load main class” mean? Post and the documents referenced. I now know the difference between .Class and Jar file. I really needed that, and wonder why this is not the first thing any Java course would cover. Steve's post was amazing, and after successfully executing the file, I wondered how we can build on this. I am not sure if there Steve would see this content, but I figured I would try since this would build on his attempt, which helped me a lot.I think what Steve's post was missing was one common project, which can provide a baseline for more clarity, because every post started with their own attempt at creating context. I figured I would try to test and learn the how different relative paths would impact Java's class path idea. Given that this project is a public project, folks can download, and use as a baseline. At the same time, it would be nice to get validation and or correction on what I found out. The other item Steve's post was missing was the use of forward slash vs. backslash and having to combine it at times to compile and execute code. Here is what I found: 1- I needed to install Maven. To my amazement, installing the Maven plugin for my IDE, did not mean that I have Maven. I am sure it was connecting a lot of very complex directories, but it would have been nice to get a notice somewhere that hey you need to install Maven.

2- This worked: Here is an example of how to run the compiled source files in this project:

C:\Users\usr\source\Java>java -cp c:/Users/usr/source/Java/algs4/target/algs4-1.0.0.0.jar  edu.princeton.cs.algs4.BinarySearch c:/Users/usr/source/Java/algs4/target/tinyW.txt < c:/Users/usr/source/Java/algs4/target/tinyT.txt
50
99
13

3- Steve mentioned something about slash being allowed instead of ".". I am not sure where that is and how; I tried using "."s and failed.

4-This worked

C:\Users\usr\source\Java>java -cp algs4/target/algs4-1.0.0.0.jar  edu.princeton.cs.algs4.BinarySearch c:/Users/usr/source/Java/algs4/target/tinyW.txt < c:/Users/usr/source/Java/algs4/target/tinyT.txt
50
99
13

but this one does not, as expected, ( I removed the alg4 from the directory path ). I wonder if it is safe to assume there should be an overlap between the directory name and the source file? Is that some some sort of a linchepin?

C:\Users\usr\source\Java>java -cp /target/algs4-1.0.0.0.jar  edu.princeton.cs.algs4.BinarySearch c:/Users/usr/source/Java/algs4/target/tinyW.txt < c:/Users/usr/source/Java/algs4/target/tinyT.txt
Error: Could not find or load main class edu.princeton.cs.algs4.BinarySearch

5-When it comes to the input args, which I guess are considered dependencies, there is less leniency, I can get away with removing the disk name I guess because C:\ is considered default, but that seems to be it.

C:\Users\usr\source\Java>java -cp algs4/target/algs4-1.0.0.0.jar  edu.princeton.cs.algs4.BinarySearch c:/Users/usr/source/Java/algs4/target/tinyW.txt < /Users/usr/source/Java/algs4/target/tinyT.txt
50
99
13

C:\Users\usr\source\Java>java -cp algs4/target/algs4-1.0.0.0.jar  edu.princeton.cs.algs4.BinarySearch c:/Users/usr/source/Java/algs4/target/tinyW.txt < Users/usr/source/Java/algs4/target/tinyT.txt
The system cannot find the path specified.

C:\Users\usr\source\Java>java -cp algs4/target/algs4-1.0.0.0.jar  edu.princeton.cs.algs4.BinarySearch c:/Users/usr/source/Java/algs4/target/tinyW.txt < /usr/source/Java/algs4/target/tinyT.txt
The system cannot find the path specified.

C:\Users\usr\source\Java>

I have downloaded a maven project form GitHub and tried to use mscode to build by opening the BinarySearch.java and clicked on "run" on top of the main(), but can not run the files in mscode due to file not found error. But the mentioned dependencies are in the same directory and namespace. Here is the message I get::

c:\Users\usr\Documents\JavaSource\algs4\src\main\java\edu\princeton\cs\algs4>cd c:\Users\usr\Documents\JavaSource\algs4\src\main\java\edu\princeton\cs\algs4 && C:\Users\usr\.vscode\extensions\vscjava.vscode-java-debug-0.23.0\scripts\launcher.bat C:\Users\usr\AppData\Local\Programs\AdoptOpenJDK\\bin\java -Dfile.encoding=UTF-8 -cp C:\Users\usr\AppData\Roaming\Code\User\workspaceStorage\cc8ab34620dad8d5d783976fbc70bb1a\redhat.java\jdt_ws\jdt.ls-java-project\bin edu.princeton.cs.algs4.BinarySearch
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
    In cannot be resolved to a type
    In cannot be resolved to a type
    StdOut cannot be resolved

    at edu.princeton.cs.algs4.BinarySearch.main(BinarySearch.java:95)

I also tried to pass the args to the file.

enter image description here

Shahin
  • 53
  • 8
  • can you share BinarySearch.java or better github url to project? Is jdk and maven on your path? – Alex Dec 05 '19 at 18:46
  • @Alex- yes here is a url: https://github.com/kevin-wayne/algs4 . Maven is installed as an extension to MSCode. It seems to respond and load different arcchitypes and what not. – Shahin Dec 05 '19 at 19:38

1 Answers1

0

It looks like you are attempting to run BinarySearch from https://github.com/kevin-wayne/algs4/ repository.

Based on your screenshot most likely you haven't imported the Maven project correctly into Visual Studio Code as the error is related to project compilation and runtime classpath. Take a look at .java vs .class question to understand the difference between source code and byte code.

Take a look at official documentation and make sure that you have the Maven integration plugin installed.

Karol Dowbecki
  • 43,645
  • 9
  • 78
  • 111