Reading file from classpath location for "current project" - classpath

i have 2 projects A and B in eclipse. B has dependency to A. In project A is a text file "file.txt". This file can be loaded in project B by for example getClass().getResourceAsStream(...)... because the location of the file is in classpath. I want to provide this same file name in project B and do some action only if file exists in project B. If there is no file in project B then no file should be found during loading with getClass().getResourceAsStream(...). Currently the file will be always found.
Thank you for help.
Kind regards
Sebastian
I need to provide a little bit more information about the problem. Sorry.
There is a project C which is the core. In project C there is class which do the following:
Check if file.txt exists. This is the base code which checks only the file name. No package name. And i dont want to change it.
if yes then do something.
if now then do something else.
A depends on C and B also depends on C. If A will be executed then the base code from C will be try to find file.txt.
If B will be executed then the base code will be also try to find file.txt. If there is no file in B project then it will found because it is in A. I dont want it.
Thank your very much.

Add the file in project B in a different classpath location, you try first the location in project B and then check the file in project A. Say the location is com.myPackage.file1 in project B and com.myPackage.projA.file1 in project A.

Related

How do you use an operation written in another file in Q#?

File A has
Operation Foo() : () {
body{
...
}
}
I want to use Foo in another operation in File B
Operation Bar() : (){
// How to use Foo?
}
File A and B may not be in the same folder.
There are two parts to making this work: namespaces and project references.
All operations (and pretty much everything else) in Q# are in some namespace. Check the namespace directives at the tops of the two files; if the namespace names are the same, you're done with this part. If not, then in file B add an open directive at the top of the namespace that references the namespace for file A:
namespace A {
open B;
If files A and B are in the same folder (same project, if using full Visual Studio), then that's all you need. If not, then you need to add a reference from project B to project A. In Visual Studio, you right-click on project B, choose Add, and then Reference..., click Project on the left of the dialog that comes up, and select project A. See https://learn.microsoft.com/en-us/visualstudio/ide/how-to-add-or-remove-references-by-using-the-reference-manager for more details.
If you're using Visual Studio Code, then use the dotnet add reference command to add a reference to project A from project B. See https://learn.microsoft.com/en-us/dotnet/core/tools/dotnet-add-reference.

Source subdirectories in Swift package

In a library package, I would like to move some source files from the "Sources" folder to subdirectories, without changing language semantics (module name, visibility, etc).
Now I have a layout like:
LibraryProject
Sources
AnotherThing.swift
FooProtocol.swift
SomeFoo.swift
OtherFoo.swift
BarProtocol.swift
SomeBar.swift
OtherBar.swift
And, if I change it to something like:
LibraryProject
Sources
AnotherThing.swift
Foo
FooProtocol.swift
SomeFoo.swift
OtherFoo.swift
Bar
BarProtocol.swift
SomeBar.swift
OtherBar.swift
Then, invoking swift build fails:
error: the package has an unsupported layout, unexpected source file(s) found: [...]
Is this layout possible? I only found this issue https://bugs.swift.org/browse/SR-66 that suggests that it is not, but I cant find confirmation (or reason) in the documentation.
Thanks
I have found two options that work for Swift projects on Linux, either all .swift files must be directly in the Sources folder, or there must be one subfolder in Sources and as many subfolders within that as you like.
Swift builds a Module out of the top-level subfolder in Sources and includes all the subfolders within that.
I don't believe it is possible to have two Modules within the same Sources folder, as a Module would not recognise any code outside itself.
So in your example a working structure would be:
LibraryProject
Sources
YourModuleName
AnotherThing.swift
Foo
FooProtocol.swift
SomeFoo.swift
OtherFoo.swift
Bar
BarProtocol.swift
SomeBar.swift
OtherBar.swift
Here is the Folder structure for Swift package
And Here i have mentioned how to add the resources and add lines in Package

Newbie IDE (IntelliJ IDEA) issue: .class files not all usable

I'm working on a school project right now, and every time I have in the past, I always make a new Project in IntelliJ IDEA. However, this time she gave us some .class files that have methods in them that we can't see (she described what they do so we know how to use them) but need to use, so they have to be in the same folder, obviously.
SIDENOTE: I'm also new to using Macs, which is what I'm on at the moment.
Anyways, I put the .class files in my src folder that I found in the Project8 folder. I just made an array of the Book objects, which was one of the .class files I moved, and now I need to use a method from the other .class file, named BookInventory.class. I put that file in the src folder just like the other, but it won't let me use the only method in that class, which is LoadBooks.
Here is the LoadBooks signature:
public static void LoadBooks(Book[] b)
And here's the description of it that she gave to us:
"For each element in the array, accepts user input for the book, creates the Book object, and stores the object into the array."
So, when I made the array of Book objects, IDEA made an import statement up top all by itself, I didn't type it:
import java.awt.print.Book;
So why does IDEA recognize the Book.class file and allow me to use it in this .java file for my project, but it doesn't seem to notice the BookInventory.class file?
Any help appreciated, thanks ahead of time.
What is happening is when you first typed the line with LoadBooks(Book[] b), IntelliJ could not "see" your class files (you have subsequently loaded them in "class files" and added that as a project library, I presume).
IntelliJ however searched for and found a Book class in the internal java libraries, java.awt.print.Book. Note that this is a different class to the one your teacher gave you, which might have been e.g. edu.myschool.homework.Book.
Firstly, try to delete the line including the import statement, or manually change it to the correct package (your teacher can inform you what it is).
If the same import comes back automatically, you can go into Settings -> Editor -> General -> Auto Import and untick Add unambiguous imports on the fly - this will cause intellij to prompt you before adding imports.
Also, I would ask your teacher to give you the class files in a jar file, since that's the usual approach.
Good luck.

Is there a way to make IntelliJ IDEA *not* put Scala source files in their Java-style package directory?

In Java, you have to put source files in the directory structure corresponding to their package. foo.bar.Baz has to live in foo/bar/Baz.java.
In Scala, that requirement is relaxed. If all your classes in a particular project are in package foo.bar, you might just want them to live in the root source directory.
But IDEA flags this as an error, and forces me to put Scala classes in their Java-style directory when, for example, I copy or move classes. Is there a way to turn off this behavior?
Go Settings -> Scala -> Other settings -> Resolve to all classes, even in wrong directories.

Keep Test classes in sync with Source classes

In Eclipse, I have two source folders, one called src the other called test. They have the exact same package structure and each src Class has a test equivalent with "Test" appended to its name. Sometimes, I move my src classes during refactoring. Is there a setting where Eclipse automatically moves the test classes as well? Also, the same with renaming source classes.
No, because eclipse doesn't know the convention you are writing about.