I am trying to create a program that fetches data from a url. When I try to debug it with VS code it outputs nothing. Even if I put "System.out.print("Something");" after the catch {} block or anywhere. Sometimes, when debugging the program that has an http api on VS code it opens a new tab with some BuiltInClassLoader.class file, highlights a section of it (it highlights line 641 of BuiltInClassLoader.class) and throws a ClassNotFoundException for the file I'm trying to run.
package a.b.c;import java.io.BufferedReader;import java.io.InputStreamReader;import java.net.HttpURLConnection;import java.net.URL;import org.json.simple.JSONObject;import org.json.simple.parser.JSONParser;import java.net.URI;public class BeaconFetcher { public static void main(String[] args) { String urlString = "https://beacon.nist.gov/beacon/2.0/chain/last/pulse/last"; try { URL url = new URI(urlString).toURL(); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setConnectTimeout(5000); connection.setReadTimeout(5000); if (connection.getResponseCode() != 200) { System.out.println("Error: Unable to fetch data from NIST beacon."); return; } BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); StringBuilder responseBuilder = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { responseBuilder.append(line); } reader.close(); String response = responseBuilder.toString(); JSONParser parser = new JSONParser(); JSONObject jsonResponse = (JSONObject) parser.parse(response); JSONObject pulse = (JSONObject) jsonResponse.get("pulse"); String outputValue = (String) pulse.get("outputValue"); System.out.println("Output Value: " + outputValue); } catch (Exception e) { e.printStackTrace(); } }}
Chat GPT wrote this code.
I have checked the classpath environment variable on my device and it does contain all the libraries and the file itself. (I might be wrong because I don't know what the classpath variable should contain.)It works fine on Eclipse though. Testing the url with the curl command on the Windows command utility also gives an adequate result. How do I make the program work on VS code i.e. print the outputValue in the terminal like it does for me on Eclipse?
Edit: In the call stack it says "Paused on exception". After restarting VS code I see the following error in the debug terminal: Error: "could not open `C:\Users\user_name\AppData\Local\Temp\cp_cv21kp9y53rhj7ktohm5sg1vq.argfile'" There is no AppData folder in the folder with my name. The ClassNotFoundException is shown in the following image: The ClassNotFoundException in "BuiltInClassLoader."
My system variable CLASSPATH
contains the following paths: C:\...\src\package_1\randomizer\*; C:\...\src\package_1\*; C:\...\.output\package_1\*
; C\JSON* (which is where the json-simple.jar file is located) along with some other ones. ;
My VS code classpath (the Project Settings one) contains the path cli/java_projects/src; (if that information is important)