I am new to both Java and to the Eclipse IDE, and I am having trouble with adding a Referenced Library and actually using it in my code.
I am in a software testing class, and my assignment is to use the JWebUnit library to run some basic tests on a website of my choice. I have downloaded a zip of the library and added it to the build path, so that it shows up in my Referenced Libraries list.
When I try to actually use that library, though, the import statement gives an error.
I just cannot figure out what I am doing wrong that the base class cannot see this library.
The screenshot show that you have added the folder net as class folder to the Java Build Path. Maybe you have added the source code (.java files) or Javadoc (.html files) instead of the executable bytecode (.class files). Usually .jar files are provided containing .class files which can directly be added to the Java Build Path.
Probably you want to download e.g. the file jwebunit-3.3-release.zip from here and add all JARs contained in that ZIP (in the root folder and in lib) to your Java Build Path instead.
Related
(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!
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.
I was recently trying to import a library to use for something I am working on.
https://sites.google.com/site/piotrwendykier/software/jtransforms
I was having some difficulty because the JAR that I added to the build path was the "JTransforms-3.0-sources.jar" and not the "JTransforms-3.0.jar".
Now I am sort of scratching my head and just wondering what that first sources.jar was if it didn't have any of the classes that I wanted to use in it.
What is it and what is it for?
That sources jar is a jar that contains only the source code (the .java files) corresponding to the compiled artifact. It is useful to add it as a source attachment in your IDE, to publish the sources, etc.
As it only contains sources, not compiled classes (.class files), it is of no use as a library dependency.
What is it: Source JARs are the JAR file which contains only source code files i.e. .java files, and no compiled .class files. For example, you can download OpenJDKâ„¢ Source files from http://download.java.net/openjdk/jdk8/
What is it for: It is useful for other developers so that they can 'attach source' and debug into their project source code.
I have following tasks:
Automatically generate java source files in the current workspace.
Compile those files after generation.
Export every generated and compiled class with needed libraries to runnable JAR file.
I already installed Eclipse SDK and I suppose what I need is to make my main class inherit some class from SDK and maybe load some other classes. But i don't know what do I need exactly and where to look. I'd appreciate some clues.
I suggest you look at M2T-JET to generate not only the Java files, but the project, any necessary folders and any other resources you need. One of those resources would be a jardesc file which is used by the JDT to persist jar export options. You can play around with those options to define the jar and export, then generate the jardesc file along with the other generated resources.
M2T-JET can be invoked programmatically, so once that single invocation generates your entire project, your plugin can make the call to the JDT to export the jar using the jardesc file.
I want to use the classes (that are .java files) which I designed separately in java and I want to add them into java library to use them in other classes with "import" keyword. How can I do this ?
Also, I should explain that I copied the classes folder in the zip file which its name is src.zip at C:\Program Files\Java\jdk1.7.0_25 but when I use the "import" keyword in the IDE it seems that there is not any folder with that name in java library.
I am not sure if I understand this correctly but you need to make a jar file from your library project and then place that jar file into the project which wants to use that library. How do you do this depends mainly on the IDE which you are using.
Add library in NetBeans
Add library in Eclipse
Add library in IntelliJ
What needs to be available to Java are the object (aka .class files). So, you can do 2 things:
Locate the directory where you .class files are and add it to the CLASSPATH (either by changing the environment variable, or by specifying switch -cp to java). Be careful with one thing: if you have a class MyClass in package stackoverflow.com, in the directory structure you will see something like .../com/stackoverflow/MyClass.class. The base folder to include in CLASSPATH is .../com, not .../com/stackoverflow .
Packaging your classes into a .jar file (pretty much a .zip, but with a different extension), and add it to the CLASSPATH as in 1. Note that n this case you use the name of the .jar, not the name of the directory it is in.
src.zip does not contain classes, but source (.java) files. That's why its extension is not .jar, and also why you don't see any effect.