I need to generate a JAVA file which will be used by the other JAVA file within the same project.
This creation will happen while building the project. Because the class is being used within the same project, hence I think it should be placed under src/main/java only.
The issue is from Paths API, I am not able to go to this location directly. I have to hard code the absolute path to my file for creating that class.
Is there any way by which I can use a relative path to create a file?
Also , Should file be there in src folder at first place?
Related
I have some .java files that I'd like to include in my build path to utilize some of the classes and such inside them. I've added them to the /lib folder but it doesn't allow me to call data types and methods and such from the .java files in my app project (in the .MainActivity). How can I accomplish this?
The simplest thing is just to add the Java source code to your existing project, alongside your existing Java code.
In your case, the Java files do not have a package declaration. That's... not good. But, you can add one, then put the files in the appropriate directory for that package in your project. For example, you could use the package used by some of your existing classes, then put the files in the same directory as those classes.
If the Java files already had a package, you could create a directory tree off of your java/ directory based on that package, then put the Java files in that directory.
i know that this question was asked several times before but none of the solutions quite work for my problem (or i just dont know how to adjust them properly).
I try to program an application, that is supposed to open an image, which is located inside of the jar file of the application.
The jar file is created by maven, so originally the picture was in the src/main/resources directory of my maven project and it will finally be in the base directory of my jar file.
The program itself consist of two java files and an fxml file. The one java file is my main class (called imageviewer.java) and the other java file is my javafx controller (called Contoller.java).
The method, that is supposed to open the picture is in the Controller.java file.
The solutions I found were all using getclass().getResources... , but in this case it does not work (maybe because it is not a jar file that will consist of a single class). The name of the final maven-generated jar file will be imageviewer-0.0.1-SNAPSHOT.jar.
How can I access the image inside it?
Okay I did actually find the solution.
In Eclipse I was only able to acess the image while it was in the same folder as the java documents. After I moved it into the "Resources" folder (which is where it belongs in a maven project) i was not able to acess it with a relative path.
All I had to do was creating a new package inside of the resources folder that is called exactly the same as the package in which the java files are.
So my project structure now looks like this:
src/main/java/myPackageName/MyJavaFiles.java
and
src/main/resources/myPackageName/Imagename.jpg
After that i was able to acess is with getClass().getResourcesAsStream("Imagename.jpg");
I have a function that requires the path of a jar file
builder.add(EventAnnotator.createAnnotatorDescription("/org/apache/ctakes/temporal/ae/eventannotator/model.jar"));
This refers to the jar file in my resource folder (as far as I can understand).
I have the same jar file in my maven local repo. and want to use it instead.
Is there a way to pass it as a string like this ?
well it seems we could directly use the path of the jar file, as per the structure of resources (if you put it there). Previously this wasn't working for me as I had a few other errors.
I am developing a Java project in Netbeans which contains multiple files with custom types (like .rml .mod and ...)
The netbeans does not show these files in project, and when I build the project I need to copy them manually so the project can load them.
Is it possible to automatically include these files into output Jar file in build time?
If yes, then how and how can I access them in code?
If no, then how can I manage these files (automatically copy them to desired place and etc.)?
create a package inside java application and add files to this package.
for access to this files use of following code:
getClass().getResource("/Images/imgedit.png")
Package name instead of "Images" and file name instead of "imgeit.png".
You can use Ant for building the project
You can take basic reference from here and here.
If your data files are in the source folder they should be copied automatically to the bin folder along with the generated class files.
To read a data file from within a jar you could use getResourceAsStream(String) available in Class class.
My file is located under the src directory. However, when I try to call it using "src/readme.txt" the file is not found.
In fact, it states that java is looking for "C:\Documents and settings\john\My Documents\Downloads\eclipse-win32\eclipse\coolCarsProject\src\readme.txt".
How do I fix this? I do not want to put in the absolute path all the time.
Do I need to fix the classpath, buildpath, or change the project root, etc? It is not at all obvious from the roughly 1000 settings in Eclipse for a newbie.
First, you have to decide if you want to load the file from the file system, or if the file will in fact be bundled with your application code.
If the former, then you should really think about how your application will be launched when actually deployed, because using a relative file path means that the program should always be started from the same location: a relative path is relative to the location from where the program is started (the current directory). If this is really what you want, then edit your launch configuration in Eclipse, go to the Arguments tab, and set the working directory you want. But the src directory is not where you should put this file, since this will copy the file to the target directory, along with the classes, that will probably be put in a jar once you'll deploy the application.
If the latter, then you should not treat the file as a file, but as a resource that is loaded by the ClassLoader, using ClassLoader.getResourceAsStream() (or Class.getResourceAsStream()). Read the javadoc of those methods to understand the path to pass. If you put the file directly under src, it will be copied by Eclipse to the target directory, along with your classes, in the default package. And you should thus use SomeClass.class.getResourceAsStream("/readme.txt") to load it.
Using paths relative to the current working directory is not a good idea in general, as it's often quite hard to establish what your current working directory will be. In Eclipse, it will be your project folder (unless you set it to something different in your launch configuration), in webapps it will be the webapp's root directory, in a command line app it could be anything.
Try this one:
String filePath = ".\\userFiles\\data.json";
where «.\» is a root for the Eclipse project, «userFiles» is a folder with the user's files inside of Eclipse project. Since we are talking about Windows OS, we have to use «\» and not «/» like in Linux, but the «\» is the reserved symbol, so we have to type «\\» (double backslash) to get the desired result.