#Turn this code from a .java to .jar

1 messages · Page 1 of 1 (latest)

cursive obsidian
#
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Scanner;


public class App {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter A stock symbol (example, BMY, TSLA): ");
        String stockSymbol = scanner.nextLine().toUpperCase();
        scanner.close();

        try {
            String apiUrl = "https://query1.finance.yahoo.com/v8/finance/chart/" + stockSymbol + "?interval=1d";

            URL url = new URL(apiUrl);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");

            BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            StringBuilder response = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                response.append(line);
            }
            reader.close();

            double stockPrice = parseStockPriceFromResponse(response.toString());

            if (stockPrice != -1.0) {
                System.out.println("Current " + stockSymbol + " stock price: $" + stockPrice);
            } else {
                System.err.println("Error fetching stock price. Please check the stock symbol.");
            }
        } catch (Exception e) {
            System.err.println("Error fetching stock price: " + e.getMessage());
        }
    }

    private static double parseStockPriceFromResponse(String jsonResponse) {
        try {
            int startIndex = jsonResponse.indexOf("\"regularMarketPrice\":") + 21;
            int endIndex = jsonResponse.indexOf(",", startIndex);
            String priceString = jsonResponse.substring(startIndex, endIndex);
            double stockPrice = Double.parseDouble(priceString);

            return stockPrice;
        } catch (Exception e) {
            System.err.println("Error parsing JSON: " + e.getMessage());
            return -1.0; 
        }
    }
}
knotty ermineBOT
# cursive obsidian ```java import java.io.BufferedReader; import java.io.InputStreamReader; import ...

Detected code, here are some useful tools:

Formatted code
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Scanner;

public class App {
  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    System.out.print("Enter A stock symbol (example, BMY, TSLA): ");
    String stockSymbol = scanner.nextLine().toUpperCase();
    scanner.close();
    try {
      String apiUrl = "https://query1.finance.yahoo.com/v8/finance/chart/" + stockSymbol + "?interval=1d";
      URL url = new URL(apiUrl);
      HttpURLConnection connection = (HttpURLConnection) url.openConnection();
      connection.setRequestMethod("GET");
      BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
      StringBuilder response = new StringBuilder();
      String line;
      while ((line = reader.readLine()) != null ) {
        response.append(line);
      }
      reader.close();
      double stockPrice = parseStockPriceFromResponse(response.toString());
      if (stockPrice !=  - 1.0) {
        System.out.println("Current " + stockSymbol + " stock price: $" + stockPrice);
      }
      else {
        System.err.println("Error fetching stock price. Please check the stock symbol.");
      }
    } catch (Exception e) {
      System.err.println("Error fetching stock price: " + e.getMessage());
    }
  }
  private static double parseStockPriceFromResponse(String jsonResponse) {
    try {
      int startIndex = jsonResponse.indexOf("\"regularMarketPrice\":") + 21;
      int endIndex = jsonResponse.indexOf(",", startIndex);
      String priceString = jsonResponse.substring(startIndex, endIndex);
      double stockPrice = Double.parseDouble(priceString);
      return stockPrice;
    } catch (Exception e) {
      System.err.println("Error parsing JSON: " + e.getMessage());
      return  - 1.0;
    }
  }
}
#

<@&987246399047479336> please have a look, thanks.

#

You can use </chatgpt:1108714622413963314> to ask ChatGPT about your question while you wait for a human to respond.

cursive obsidian
#

please turn that from a .java to .jar

#

then send it here

thick delta
#

u want a runnable jar?

dense glacier
thick delta
#

we can show u how to do it urself, perhaps that's better on the long run?

cursive obsidian
cursive obsidian
thick delta
#

do u have an IDE like intellij?

cursive obsidian
#

im using VSCode

#

😭

thick delta
#

okay, vsc is generally rather bad for java. but afaik it can create runnable jars as well

dense glacier
#

you could do it from command lines

cursive obsidian
#

i tried

#

from like googling the commands i should use

#

i got sum errors

dense glacier
#

show them please

cursive obsidian
#

one sec

dense glacier
#

and show the commands you used

thick delta
cursive obsidian
#

first it told me to use

#

Bash
javac App.java

dense glacier
cursive obsidian
#

where i got that

dense glacier
thick delta
#

(the vsc ui can do it with a few clicks without commands)

cursive obsidian
thick delta
#

yeah as shown in the article i just linked

#

but ofc it ultimately only uses commands under the hood. commands that u could also use manually

cursive obsidian
#

my calc dosent show up

proper carbon
#

IntelliJ CE is also free =>

knotty ermineBOT
dense glacier
cursive obsidian
#

o

#

k

#

ik

dense glacier
cursive obsidian
#

oo it worked

#

yea

coral basalt
cursive obsidian
#

one more thing

#

i tried turning the .jar to a .exe

#

using Launch4J

#

it dosent work

#

like the exe got made

#

but when i launch it

#

nun happen

coral basalt
#

its like a console only application right?

#

I think you might need to do some setup in launch4j for that

#

but not sure

cursive obsidian
cursive obsidian
#

it might be a issue with the .jar as i wasnt able to run that either

coral basalt
#

uh yeah most likely

#

you tried running it from console?

#

java -jar file.jar?

cursive obsidian
#

nope

coral basalt
#

try that

#

and see if you get an error

cursive obsidian
coral basalt
#

the -jar needs to be together

#

no whitespace

cursive obsidian
#

it works

coral basalt
#

good

cursive obsidian
#

yeah

coral basalt
cursive obsidian
#

do i type that into my console?

coral basalt
#

first do you have a config for generating the exe?

#

well

cursive obsidian
#

it auto created one

coral basalt
#

I think this is not helpful actually

cursive obsidian
#

when i used launch4j

coral basalt
#

I missunderstood the docs I think

cursive obsidian
#

o lol

cursive obsidian
coral basalt
#

on this screen

#

try selecting console

cursive obsidian
coral basalt
#

?

cursive obsidian
cursive obsidian
#

thing

coral basalt
#

dont do that

#

use launch4j

cursive obsidian
#

alr

cursive obsidian
#

is this what could be causing it

#

wait

#

i jus realizied

#

that sais JRE

coral basalt
#

it depends

cursive obsidian
#

not JDK

coral basalt
#

thats fine

cursive obsidian
#

does it matter>

coral basalt
#

only JRE is needed to run a jar

cursive obsidian
#

i coded it in JDK 21

coral basalt
#

JDK is needed for developer

cursive obsidian
#

im gonna try this

coral basalt
#

because?

cursive obsidian
#

idk

coral basalt
#

see

#

so dont

#

lol

cursive obsidian
#

the guy from youtube did

coral basalt
#

check in console which java version you got

#
java --version
cursive obsidian
coral basalt
#

so version 21 should run fine

cursive obsidian
#

k

#

so should i try making that exe now

#

or are their any othe rsettings

coral basalt
cursive obsidian
#

i did

#

i read that alrdy

#

it workss

#

ty

coral basalt
#

nice

cursive obsidian
#

oop

#

it crashes

#

cuz the code is sopose to be a current stock price finder

#

but once i put in the stock symbol

#

it crashes

coral basalt
#

I doubt that, I guess it just finished running and thus closed the program

cursive obsidian
#

is their anyway

#

i can keep it alive longer

#

so its possible to see the values

coral basalt
#

uh I think you could technically put a scanner.nextLine() a the end to bypass that

#

and after user presses enter its going to close

#

not sure if there is a different way (official way)

cursive obsidian
#

damn alright

#

one sec ill try that

coral basalt
#

also I would prefer using the official tool jpackage instead of launch4j

cursive obsidian
#

o damn

#

i dont have that installed

coral basalt
#

but both works here

coral basalt
#

its in the JDK

cursive obsidian
#

ohh

#

alr

#

i updated the code

cursive obsidian
#

i can get a gui in

dapper wren
#

if u code it in , u can just convert it again 😉