1

enter image description here

So I'm a new java developer and VS code is currently the platform I use. To my understanding every time you create a new object in Java, a class file should be generated as well. As explained in this video { https://www.youtube.com/watch?v=dFuVh_Bzy9c&list=PLsyeobzWxl7pe_IiTfNyr55kwJPWbgxB5&index=50&ab_channel=Telusko beginning from min mark "3:00" } My problem is that whenever I run my program it doesn't create a class file automatically even when creating a class file manually. When I try to manually create a class file I get this message,

"This file is not displayed in the text editor because it is a Java class file. Click here to decompile and open."

After I decompile I get this message "Error while getting decompiled source."

I just really need some advice and help I've been trying to figure this out for about a week now. I tried configuring the paths, installing/uninstalling extensions, reinstalling the JDK. I guess I really just don't know what I'm doing can someone please help me.

The image provided is an example of what happens every time. This only pops up after trying to manually configure a Class file. It doesn't configure automatically.

starball
  • 20,030
  • 7
  • 43
  • 238
subcoldmay
  • 13
  • 2
  • If the code is running, then at least one class file was created (even if it's in memory only). My guess is VS Code is simply putting those class files somewhere else that you can't see. For instance, if I just "Run Java" with no other configuration, it seems to put the class files somewhere in my `AppData/Roaming/Code` directory (on Windows). I don't use VS Code for Java development much, so I don't know how to configure it to put class files in the project directory (e.g., in a directory named `out` or something). I also tend to use a build tool like Maven or Gradle, anyway. – Slaw Jul 06 '23 at 05:21
  • And note, you should _not_ be creating class files "manually". – Slaw Jul 06 '23 at 05:23
  • the class file is not created when running the program. it's created when you compile the source into the class file with something like javac or whatever build tool you use that wraps around it. – starball Jul 06 '23 at 05:32

2 Answers2

0

Do you want to compile or decompile? I think you need to compile. Fix the question title if this is true.

As a experienced user in netbeans, eclipse, intellij and visual code, I recommend you to use eclipse for java development. Don't use visual code. That ide is perfect for c# and javascript.

If you choose eclipse, I can help you to configure your workspace.

Compile java with vs code

Anyway if you want to use vs code, basically you will need to try plugins and configure them :/

Compile with cli

Since you are trying simple classes, you could use the shell to compile and run your java class. This method is the ultra fast way to do it, even faster than Eclipse

  • install java in your machine
  • validate with java -version
  • compile with javac Please.java
  • run with java Please

enter image description here

You could continue using vs code to develop and the shell to compile like a pro.

JRichardsz
  • 14,356
  • 6
  • 59
  • 94
0

VS Code doesn't create Java class files automatically. If you want to create a class file just run
'javac Please.java'
this will create a Please.class file
You can now run this file by typing 'java Please' in the terminal

For Ex: Creating a sample HelloWorld java file creating a sample hello world java file

when I created this file and run it using the run button present in the top right corner, It just output Hello World and does not create any java .class file.

But if I want a java .class file, I can create It using javac.
For that I just wrote in the terminal 'javac HelloWorld.java' enter image description here

And now the .java file is compiled into a .class file
When I click on Decompile Class File
It just decompiles .class file into a Java source file
enter image description here

Pulkit Pareek
  • 53
  • 1
  • 5
  • Hello and thankyou for your response. I typed javac and it created Class files. However It brings me to the same decompile screen and then gives me the same error just as my image shows. What should i do to troubleshoot this? – subcoldmay Jul 06 '23 at 06:11
  • Restart VS Code and try again. – Pulkit Pareek Jul 06 '23 at 06:19
  • Thankyou So much man you just saved me so much frustration and headache – subcoldmay Jul 06 '23 at 06:22
  • I'm glad. Don't forget to upvote and accept this answer. – Pulkit Pareek Jul 06 '23 at 06:25
  • I got you, when im eligible to upvote I wont forget you. Thankyou again – subcoldmay Jul 06 '23 at 06:36
  • "_when I created this file and run it using the run button present in the top right corner, It just output Hello World and does not create any java .class file_" -- It did create a class file, just not in the project directory. The Java source code must be compiled first before the resulting byte-code can be executed by the JVM. – Slaw Jul 06 '23 at 08:14
  • In my context, I was referring to the project directory. – Pulkit Pareek Jul 06 '23 at 08:20