I have three .class files that I'm supposed to black-box test. They are under a package named one.two.three. I'm having difficulty accessing them. I started a new java project in eclipse and created a package with the same name. I then proceeded to add the three .class files to bin/one/two/three. If I try using the classes Eclipse can't find them and I get compiler errors. I'm using the appropriate package header. Any help is greatly appreciated, thanks.
assuming bin/ is your output folder in the Eclipse project settings. Simply dropping the class files under bin/one/two/three will not work as eclipse will either delete them (on clean build) or just ignore these extra artifacts. Do as #Ray Tayek says- keep them in a different directory and edit project classpath and add this as class folder. This should work.
Related
I have the following line of code in a .groovy file for testing:
GenerateShipConfirmsForBatch gscb = new GenerateShipConfirmsForBatch();
Ctrl-clicking on the GenerateShipConfirmsForBatch takes me to GenerateShipConfirmsForBatch.class in a .jar, and not the .java file, even though I have the class correctly imported at the top. I want it to reference the .java file so it will pick up changes I make to the .java file.
Any help would be greatly appreciated!
First guess - Wrong import
What you wrote seems to me like you have class with the same name in 2 different packages or in imported project instead of in open-able java class. When you import class be careful that you import the one you want to use.
Eg. annotation class Service is wildly used in different packages.
import org.springframework.stereotype.Service;
import com.google.web.bindery.requestfactory.shared.Service;
Just check that you are using the correct import.
Second guess - Incorrectly set modules
If you are having the multi-module application you have to set correctly the parent project to properly address this issue as well as child projects where the links should be as well.
In Maven it is done using pom.xml. It is very nicely addressed in Maven - Guide to Working with Multiple Modules.
In Gradle it is done using build.gradle. You can read more about it Gradle Multi-Module Project Setup.
Basics about classes
Local class
Idea is linking local .java files in preference instead of .class therefore if this is happening I'd recommend reinstalling Idea as I cannot find the correct approach.
Linked class (from external library)
If you have imported external library it WILL link to .class as it is decompiled from .jar file.
What you can do is either download .jar with source codes, if you are using Maven Projects click on Download Sources and/or Documentation.
Just because you have the class imported at the top does not mean that you can view the source code (e.g., .java file). If this class is coming from a dependency defined in your pom.xml or build.gradle file then you likely won't have access to view the source code. However, if this is a separate module you have at the top level of your project, then you'll be able to view the .java file. If this library is open source then I'd suggest cloning it in your project and adding it as a module. That will solve your problem.
You can install Java Decompiler IntelliJ Plugin from here: https://plugins.jetbrains.com/plugin/7100-java-decompiler-intellij-plugin
It allows you to display all the Java sources during your debugging process, even if you do not have them all
I just started taking this Distributed Systems class, and my teacher says the following on our class website:
Please do NOT use packages! If your configuration of Eclipse uses them
by default, please remove them before writing code!
What the heck is she talking about? I thought eclipse NEEDS to create packages for your project source files to stay organized. How do I remove them?
Packages are not mandatory. If you create a new project in Eclipse, you can directly add your classes to src folder. These files will be under /workspace/project_name/src/ folder. If you add a package, your class files will be placed under /workspace/project_name/src/package_name folder. Your instructor will need to know package names to be able to compile your files and she is probably using a script or something to automatically compile them and that script assumes your files are placed under src folder.
Just remove the first line of your code if exist that says:
package <package-name>;
And if you are creating a new class, then don't give any package name.
I think this is a super easy question, but im new to this and not sure what to do. So I have added an external jar file into one of my projects, and was able to decompile the classes using JAD to see some of the class files, the thing is I want to add a new class to one of the directories in same JAR file, compile it and update the jar file. I was googling and I came up with things like an 'ANT build file' but not sure how that would work?
Thanks!
First of all i would check out if the developper of that JAR has provided the source to the classes. This reduces the possibility of JAD not beeing able to fully decompile all the class information.
However once you have all the .java files (including your changed or added class) you can let eclipse build the jar file as follows:
1.) Compile the .java files (Project -> clean..)
2.) File -> Export -> Java -> Jar file (click next)
3.) Select anything that belongs to the jar, input a name and click finish (Note the checkbox that says to export generated class files and resources).
Regarding Ant/ Maven and stuff: Those are just tools that help you. The same result you can archive by opening a console and use javac.exe to compile you can use ant or maven to compile your classes. Its just a matter of configuration.
I recomend you skip Ant right here and directly do the Maven in 5 Minutes tutorial :)
Your approach works. But you can have two classes with same name and package. This will remove the necessarily of updating the jar file.
You must set the loading order to make sure that your new modified version will be used instead of the one which is in the jar file.
Possible to use two java classes with same name and same package?
Before any tweak and class loader, test this approach, may be the default class loader behavior will do what you want.
I have a JAR file that contains a series of .java files/stub code. I need to include the JAR into a NetBeans project and call the various methods contained within. I've attempted Solaris as well, but no luck there either.
At the moment, the entirety of my Main.java (the only src file in the project) is:
package TestApplication;
import edu.university.department.ws.DiscoveryServices;
public class Main {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
The exact error is:
C:\code\Projects\WebServiceTest\TestApplication\src\testapplication\Main.java:3: package edu.university.department.ws does not exist
import edu.university.department.ws.DiscoveryServices;
1 error
I added the JAR file by going to libraries, clicking Add JAR/Folder, and selecting the JAR. Once it was added, I can drill down into it in NetBeans and see all the stub code, including edu.university.department.ws.DiscoveryServices.java, but yet it can't seem to import it.
I also tried going to Tools -> Libraries then adding the JAR to the classpath there. It didn't change anything.
Is it possible the JAR file wasn't created correctly? I can see the code in NetBeans, so I don't think anything is wrong with the JAR, but I'm new at this and not sure what I should be looking for.
Thanks!
Try this:
http://gpraveenkumar.wordpress.com/2009/06/17/abc-to-import-a-jar-file-in-netbeans-6-5/
Hope help.
Does your Jar really contain .java files? It looks unusual, because java-files should be compiled to .class files before packing to Jar.
Maybe http://docs.oracle.com/javase/tutorial/deployment/jar/ can help you.
Haven't you seen some warnings while installing the jar?
I have seen that message when I had erroneously the same jar installed twice - as compile library and as a test library.
As the result, many of packages became invisible.
After removing the jar from compile libraries everything become visible.
One can understand, why the such a problem may comes from.
I have had this problem, when used in Netbeans IDE the java SOURCE code using
pasting code from the other internet source by Ctrl-C Ctrl-V key combination.
(Pasting from other IDE).
The reserching of prombled showed that the problem is of absence of
statement "package nameofpackage" in code, that you want to use as library.
So, to correct the mistake you need to insert the statement package in project you want to use as library.
Perhaps, you need also to delete and link library in Netbeans again.
To link library I used the the menu point "Add library as JAR file" from tree menu item in project "Libraries" and rignt-mouse click - then "add library"
I have some classes in my current project which have the wrong package declaration (they are in the wrong folder for their declared package.)
Unfortunately, fixing the problem by moving the class is not an option. Is there a way I can get eclipse to ignore the error?
You can fiddle with Eclipse's display in the "Errors/Warnings" section of preferences, but Java requires that you have files in the correct folder to match their package, so it won't be able to build
If fixing the problem by moving the class is not an option, is fixing the problem by changing the declared package name an option? If you can't do one of those two things, the code won't compile -- so this wouldn't be an Eclipse problem so much as a Java problem.
Do you have the source folder for Eclipse at the right level? The source folder for Eclipse should be at the level where the package folders start. If there are multiple folders with separate package structures, each one would be it's own source folder.