Configure VS Code to use .jar files when doing live syntax checking - java

How do I configure VS Code to use the .jar files in my lib directory when doing live syntax checking? I can compile and run my code without any problems using the "Debug" menu in VS Code; but the editor is marking every reference to JUnit classes/methods as a syntax error.
I have a very simple standalone Java "project": One source file, one JUnit source file, and a lib directory containing the JUnit and Hamcrest .jar files. As suggested by this document: https://code.visualstudio.com/docs/java/java-project, simply placing the .jar files in a lib directory should be sufficient. It is when it comes to compiling and running the code; but, my editor is full of red squiggly lines.
Update: I tried refreshing the workspace cache, and now it won't even compile and run. Am I misunderstanding the directions; or, so I have a setting messed up somewhere?

Related

Why does VSCode keep opening *.class files as opposed to .java files?

(I couldn't figure out how to upload my screen capture to stackoverflow. So this is a streamable link: https://streamable.com/0im8tx)
In this video, VSCode opens QueriesController.class as opposed to QueriesController.java when I cmd click into QueriesController.
I have compiled provided the definitions of the jar file in my workspace:
"settings": {
"java.project.referencedLibraries": {
"include": [
"<path-to-jar-that-contains-QueriesController.jar>",
....
"sources": {
"<path-to-jar-that-contains-QueriesController.jar>": "/my/local/java/definition/src/folder",
Does anyone know why VSCode is choosing to open the definition as a .class file rather than a .java file?
I use commands to generate a simple jar package and use it in another project. It's true that when we click the class name, .class file is opened instead of .java file:
About how to generate a executable jar package, you can have a look at this reply:
Compile .java file and generate .class;
Generate manifest and pack them into jar
In general, a JAR (Java ARchive) is a package file format typically used to aggregate many Java class files and associated metadata and resources (text, images, etc.) into one file for distribution.
.java file isn't included in jar packages, and that's why you get .class file opened instead of .java file.
I am not familiar with VSCode but your problem is common across most IDEs.
Usually when a jar is made, it consists of compiled class files rather than original source codes. The reason for this is to run code as efficient and fast as possible and usually people don't want source code in jar because when running they also have to be recompiled again which is a waste of time.
Take a look at this picture. I have just downloaded a jar file from mavenrepository and it downloads the compiled version of jar. The extension is .class
What the IDE does is it tries to decompile the code with a decompiler (In this case as you can see FernFlower decompiler).
However it lacks formatting and in-code documentation the source code (.java) has. Which is why most IDEs offer to download sources. Intellij shows this right on top. Other IDEs may have this setting buried in deep. (You may have to check for yourself)
When you download sources, IDE try to contact the server and download original source code. Probably that would look something like this:
If you look closely you can see name has changed to .java which represents the source code.
VS Code has option under Java Settings, Java Download sources and Maven download sources.
It is not enabled by default. Upon enabling it, VS Code shows the proper source file, although the name appears to be .Class files.(Upon Ctrl + Clicking the symbol, with method implementations, comments, etc.,JavaDoc Comments)
If proper sources are not found in m2 repository, it shows the decompiled class file with stubbed methods. A comment similar to this is shown at the beginning of the file.
// Failed to get sources. Instead, stub sources have been generated by the disassembler.
// Implementation of methods is unavailable.
In Either of the cases, VS Code shows the maven library files as .Class files in read-only mode. Also, source files are not displayed on the Java Project Explorer.(Although even if it exists in the local .m2 repos).
Hope that helps! Happy Coding!

VSCode java how do I set the path for the compiled .class files to go?

I'm using the Java Test Runner extension in VScode, and I want it to compile my classes to a specific folder. I found the setting to change the -cp when actually running the compiled .class files, but the whole thing still fails since the extension builds the .class files in an obscure temporary folder deep in it's appdata folder.
I would like it to compile my .java classes into a folder I have within my project, so that I can have them there. I have tried googling the sh*t out of this and either I have no idea what I'm saying or noone has ever had a problem with this before.
Thanks in advance for the help.
This is a late answer, but I just ran into this so I hope it helps others.
As of Feb 4, 2021 there is now a setting for the Java Language Support for VS Code extension, where you can specify the output directory for compiled .class files. If you're using VS Code to run Java I'm almost certain you'd have this extension installed.
Simply open your VS Code settings and search for java.project.outputPath. As you'll notice, it states this setting only applies for Workspace settings, not User settings. So make sure to switch to the Workspace tab before clicking Edit in settings.json. This will edit the settings.json file in your projects .vscode folder.
Also note this doesn't work for projects managed with Maven/Gradle. It's great for simple projects though.
For the curious: link to the GitHub pull request where the feature was added to the extension (also has a neat gif of feature in action): https://github.com/redhat-developer/vscode-java/pull/1694
https://blog.usejournal.com/visual-studio-code-for-java-the-ultimate-guide-2019-8de7d2b59902
Under "Adding Libraries and JARs"
First of all,
you can create a CLASSPATH location in your environment variable section, after you set your path location.
Also, what people usually mistake is that they install a code runner used to compile and run all the languages, so if you are running your java code through the top-right run button,
then click on the drop down menu beside it and click the "run java" button and I guess then your code will run just fine.(if you have downloaded java runner extension separately)
I hope this helps!

Cannot open java programs I saved in eclipse

I had no problem running my java code in eclipse last week and I have no problem creating a new java application in eclipse. However, I am unable to open any of the java program files in eclipse that I saved. I checked to see if my jdk se development kit was removed from my computer by mistake and it is still there. What do you think is wrong? Why can't I open my old files? All of my java programs are saved as CLASS files.
Source files are normally saved with a .java extension. Once you compile your source files, javac will generate you some files with a .class extension. These are not source files, and are usually deleted and recreated every time you compile your code. If you want to find your source code, you'll need to look for the .java files on your disk.
If you've deleted your .java files by accident, it is usually possible to decompile the class files into something resembling the original source, but much of the original formatting will be lost - comments etc. This approach is far from ideal, but may help you recover the situation if you cannot recover the original source files. A good decompiler can be found at http://jd.benow.ca/ - you can either download the standalone application, or it has plugins for Eclipse and IntelliJ.
Hi once you compile the source java files .java extn the class files will be created. Check your workspace in your disk there will be a folder as package as you created in eclipse under src folder.

RobotFramework cannot import Java keyword library

I did some robot framework python examples with pybot, and referenced .py files as my library files. The folder structure I used was test/lib/myLib.py and test/test/myTest.robot, where /test was at the same level as the /src folder in my project in eclipse.
I then tried to do similar things with a java project using jython. I have /test/test/myTest.robot which imports the library ../lib/myLib.java. I even tried importing this file using RIDE, but it always shows up as red text, meaning the import failed. The specific message I get from using jybot on the command line is:
"Importing test library 'C:\Users\cody\git\myProject\test\lib\myLib.java' failed: ImportError: No module named myLib"
I read that I might need to add it to classpath, and I think in order to do so, I need to make it a .jar file. I'd rather not do all that if possible to just leave it as a .java file. I attempted to add the lib folder to the build path... By that I mean I added the /test/lib folder to the "Source folders on build path". I also exported the darn thing as a jar and added that as a library. All this was done from the project properties in Eclipse.
I'm pretty stuck... any ideas how to get this working?
Java being a compiled language, you need to compile your java Class before importing it with RobotFramework.
Normally, Eclipse would do that for you, and put the resulting .class files inside a bin repository. If not, you can issue the javac path/to/file.java command, and move the resulting .class file where you want it (somewhere referenced by the classpath.
From within the .robot file, you should have the line Library test/lib/myLib, without neither .java nor .class at the end.

Make eclipse copy groovy spring files rather than compile into class files

I am writing my first Spring application with Spring 4 groovy configuration. I am using Eclipse IDE. As usual I have all my Java files (actual domain/services etc.) in src/main/java and all my Spring Groovy configs in src/config/groovy.
Both are added as source to the classpath in Eclipse. As a result Eclipse compiles my groovy files into .class files and copies into target folder. But the problem is I am trying to load my Spring groovy classes from classpath as below, but it doesn't work as JVM can't find .groovy files as Eclipse compiled them into .class files.
def ctx = new GenericGroovyApplicationContext()
ctx.load("classpath:spring/SpringConfig.groovy")
Is there any way to force Eclipse not to compile my Spring groovy files but copy them to the output folder like xml files? For a test application I can probably use the file system path like "file:..." but I don't think I want to use it in PROD, so I am trying to write something that works both in IDE and PROD. Any suggestions? Thanks.
Ok, I should have looked around a bit hard! Resolved it myself.
In Eclipse, Go to Window->Preferences->Groovy->Compiler
Just check the option "Enable script folder support" and the pattern to cover the path where your groovy scripts are, then eclipse just copies them to the output folder like any other scripts.

Categories

Resources