Reading a java file from different project - java

I have two different java projects , and im trying to read one java file in another java file in different project.
eg: i have two projects project1 and project 2. They have packages named package1 and package2 correspondingly. I am trying to read a java file lets say JavaFile2.java which is in package 2 in JavaFile1.java which is in turn in package1 of project1.
I only know that i have to read package2.JavaFile2 , how do i achieve this ?
Need help Badly :(
Thanks

You will have to use either file path relative to project2 or, what seems to be safer option, use absolute file path when opening the file. Overall doing such kind of cross-reference between projects is not a very good idea as you will be tight to the relative location of the two projects.

Iam unable to understand the reason why you want to do so.
However, in the runtime both project1 and project2 will not have .java files available in JVM
having said that, the only option is to include the /src folder in your project archive (jar/war/ear) and writing an adapter which provides the resource from archive is seemingly possible.

You have to add a dependency to the project1 which specifies that the project2 is also on the classpath. Since you haven't specified IDE or anything else, there's nothing I can do right now...
You may try Maven for dependency management
http://www.mkyong.com/tutorials/maven-tutorials/

Well you can use getClass().getProtectionDomain().getCodeSource().getLocation().toExternalForm() to get the location of current project and "navigate" to the other project.
But like said before...I do not understand why you need such cross-referencing.

Related

Can't make a folder in Eclipse

All I want is to make a folder at the "root" of my eclipse stuff so I can store projects from different classes together. However, it won't let me do that, the "Finish" button is greyed out until I select a parent folder - I must choose from one of the many projects I want IN this new folder. What do I do?
It kind of sounds like you'd be best served by a single project that contains all of the source files from your classes. The workspace is the root directory and any subdirectories are expected to be individual projects.
I agree with what everyone has been saying, create a project for the entire class as 1 project. Then you can create as many classes as needed with main() and only export that single class (or dependent classes as needed) to turn in. In addition, you could package each individual project assignment in a separate package. As stated above, leave your root (workspace) where it is.
Instead of creating multiple projects you may use Java packages option. Create a new package and put all your additional classes over there. Please note that all Java files should be under src/ directory of the eclipse project.

Adding a class to an exisiting Jar files - Eclipse

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.

Regard creating a Java project in Eclipse

In the “create a Java project” wizard.
For the “project layout”, there are two choices:
1) use project folder as root for sources and class files.
2) Create separate folders for source and class files
Which one should I choose?
For the “Working set”
Whether I need to check the “Add project to working set”? What does it mean?
I always choose Create separate folders for source and class files, it's just separate your src files and your output files
The one you choose is up to you. It doesn't matter one way or another, at least as far as your tools are concerned.
The first option means that all files will be in the root directory of the project (typically PATH_TO_WORKSPACE/projectName). Your .java and .class files will be here if you choose that option. The second option will create PATH_TO_WORKSPACE/projectName/bin and PATH_TO_WORKSPACE/projectName/src. Your source files will be in /src and your compiled files will be put into /bin.
My personal preference is to not use the project folder as the root for sources and class files and to create separate folders for source and class files. However, it's all up to you.
In my opinion, choose different folders for sources and binaries. It will make source control and versioning easier.
Working sets only make sense when you are using more than one project for one workspace. I would guess that you won't need working sets until you are more experienced with Eclipse.
it is only a metter of user convinience. eclipse is able to handle both ways.
working set is a way to handle eclipse workspace when you have many projects. to get started, you don't need that.

How to create a folder in Eclipse?

I'd like to create a folder under a package in Eclipse... The purpose of this folder is merely for organizational purposes. Meaning, I don't want it to be another package. Every time I try to add a folder under a package, it just creates a package instead. I'd like to have the following structure:
project/src/package1/someClass.java
project/src/package1/someFOLDER/anotherClass.java
project/src/package1/package2/anotherFOLDER/oneOtherClass.java
Is it possible to do this without adding a package? I come from a .NET/C# and C++ background... here I'd just add a folder and the reference to that class would be updated in the project.
How can I just add an organizational folder in eclipse? Thanks
Actually, folders are packages in Java so your question doesn't really make any sense in a Java context.
The term 'package' might be misleading... a package is definitely not the same as an assembly in .NET. You can think of 'package' more as a namespace, which in the Java world happens to be determined by the directory path.
For non-source files, you can create a new folder outside of your source folder, it will then be treated just like any other folder and not a package.
WTF are you supposed to do when you need multiple classes to be package private in a large project? Just look at your 25-30 class files all in the same directory?
That is a problem with Java's package system. Every package is a directory, and sub-packages are just different packages (no special visibility rules).
The most coarse visibility level is package-private, so that, yes, you have to lump your 25-30 files into the same package to avoid universally public visibility.
OSGi addresses this issue by introducing bundles, which can choose to not make packages visible to the outside. This gives you "project-private" packages.
Update: Also, you can reduce the number of files by putting related classes into the same source file. Only public classes need to have their own source file (I do prefer to have one file per class, though, public or not).
Well packages ARE folders.
If you want Eclipse to not build the contents of the package/folder, right-click on the project, select Properties, and under Java > Build Path edit the inclusion/exclusion filters.
If you're creating the folders to separate classes for organizational purposes, but still want them to be in the same package for access purposes, you can create multiple source folders and have them build into the same location. So, the folder hierarchy will look slightly different:
project/concern1_src/package1/someClass.java
project/concern2_src/package1/anotherClass.java
project/concern3_src/package1/package2/oneOtherClass.java
As I said, all three source folders can build into the same target directory. Or they can build into different target directories, if you like. The separate source folders will also carry over to the Package Explorer.
In eclipse the src folder is the main Source folder. First you just remove from the build source by right click the src folder and choose build path-> "Remove from build path". After that create the folder under src and right click the subfolder (package1) and again select buildpath->Use as Source Folder. Now you get src/package1.

How do you add a directory of java classes to a project?

I have a Java project I'm working on, and wish to include a directory full of classes. These are the "JEdit Syntax" classes, and come within two packages:
org.syntax.jedit
org.syntax.jedit.tokenmarker
However, everywhere I look it tells me to "import the entire jar file". My problem is that there is no jar file, just a directory with a subdirectory, both filled with *.java files, each containing a class.
In Netbeans 6.5 I added a library by "Jar/Folder", and both appear in my new library I created, but when I go to import the two packages listed above, I get the error that "org.syntax.jedit does not exist (cannot find symbol)".
Can anyone show me what I'm doing wrong?
Thanks a ton.
It sounds like you have tried to add the two packages to your classpath seperately - and at the wrong level.
If you are pointing at a folder, you have to point to the "root" folder of the package hierarchy - ie in this case the folder which is the parent of "org"
Then from there it will look down the package/folder hierarchy org/syntax/jedit to find your classes.
So if your files are in the directory "c:\mylib\src\main\java\org\syntax\jedit" then you need to point the compiler folder at "c:\mylib\src\main\java".
That should then find all the classes.
I agree with evnafets, you probably added the org directories, but you should have added the directory that contains them. You can also build a jar out of those directories easily if there is an ant file (build.xml).

Categories

Resources