Jump to content

How to run a Java program in Visual Studio

I'm trying to run my old Java code in Visual Studio and I installed the Java Language Support extension but when I try to run it, the command window quickly opens and closes. It does it in a flash so I don't know what the output even looks like. Is there a Java equivalent to C++'s System("pause"), is there some settings I need to configure? What can I do to make VS my Java IDE?

Link to comment
Share on other sites

Link to post
Share on other sites

If your running it in debug config just use a break point. I don't know enough about java to give a very detailed answer.

CPU: Intel i7 - 5820k @ 4.5GHz, Cooler: Corsair H80i, Motherboard: MSI X99S Gaming 7, RAM: Corsair Vengeance LPX 32GB DDR4 2666MHz CL16,

GPU: ASUS GTX 980 Strix, Case: Corsair 900D, PSU: Corsair AX860i 860W, Keyboard: Logitech G19, Mouse: Corsair M95, Storage: Intel 730 Series 480GB SSD, WD 1.5TB Black

Display: BenQ XL2730Z 2560x1440 144Hz

Link to comment
Share on other sites

Link to post
Share on other sites

16 hours ago, NovaGear G2 said:

I'm trying to run my old Java code in Visual Studio and I installed the Java Language Support extension but when I try to run it, the command window quickly opens and closes. It does it in a flash so I don't know what the output even looks like. Is there a Java equivalent to C++'s System("pause"), is there some settings I need to configure? What can I do to make VS my Java IDE?

Sadly, there isn't a simple built-in function in Java to achieve this. You'll need to make one using the scanner and nextLine(). An example being...

 

//You can put this where-ever appropriate, so long as it's in the right scope
Scanner input = new Scanner()

//Asks for user input before continuing
void PauseForUser()
{
	System.out.println("Press any key to continue");
    input.nextLine();
}

It's not perfect, but it'll do in a pinch.

CPU - Ryzen 7 3700X | RAM - 64 GB DDR4 3200MHz | GPU - Nvidia GTX 1660 ti | MOBO -  MSI B550 Gaming Plus

Link to comment
Share on other sites

Link to post
Share on other sites

Breakpoints work in debug mode like trag1c suggested, but you can also run without debugger and it won't close the console window on you. At least it works that way for .NET console apps.

 

How good is VS for Java development anyway? I assume it's ok but the extension page says

Quote

This extension is not meant to replace a full IDE, but has certainly helped when I wanted to quickly reference a Java source file while working within Visual Studio.

Seems to me like you'd be better off using a proper Java IDE like IntelliJ.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×