I am trying to resolve and invalid package name currently my package looks like this:
-com
--domain
---app
----domain
so the package name is com.domain.app.domain
It needs to be com.domain.app, I have tried to move the contents of ----domain, into ---app but I get an IOException and am told certain directories cannot be moved?
I first get warnings Multiple directories correspond to package, these directories will be moved and all references changed, I select 'Yes', I then select refactor and I see the refactoring preview, I select do refactor and I get java.io.IOException: Cannot move 'and the a reference to the first directory here'.
How else can I resolve this, if I cannot move the files or delete the package?
I think you would find it quicker/simpler to move them manually and replace the package names using a 'Find/Replace in file' tool:
use the file manager/explorer to move the contents of com/domain/app/domain into com/domain/app and delete the now empty 'domain' leaf directory
use the find/replace in files function (in any decent text editor) to replace
'package com.domain.app.domain' with 'package com.domain.app' in all your source files.
change the applicationId in app/build.gradle & AndroidManifest.xml (if you want to - they don't need to be identical to the actual package name in the sources)
Get back to what you actually wanted to do ...
I've always found that the refactor in AS more trouble than it's worth.
Related
I used to have all my files in one folder of each type but recently I found an option do it in multiple (at least I think so) - orig. question stackoverflow
I tried to do it the way they did in the answer but there was a note on android site that you should avoid specifying a directory which is a parent to one. And so I did the following:
The code doesnt fail grandle sync but I am not sure if it works because even if I change the name of "mipmap" to something that isnt in the res folder it will sync anyways..
Is this the correct way of writing sourceSets for multiple folders of the same type?
Should I add the srcDirs add to each directory in res folder meaning font/layout/menu/raw/values
Is this the correct way of setting a source directory for mipmap which has multiple qualifiers?
Thanks for any help
EDIT : #MEETPrajapati This error popped in one of the drawable files once I synced it.
You can make a new drawable folder inside new res folder called "module1" like this :
sourceSets{
main {
java.srcDir("src/main/java")
res.setSrcDirs(["src/main/res","src/other/module1"])
manifest.srcFile("src/main/AndroidManifest.xml")
}
}
I have a class within a Maven project that I am trying to use to get user data and map it to a json file located in another folder outside of the one the compiled jar is located.
My question isn't necessarily how to append data to a json file, but rather how I can get the location of the json file I'd like to append my data too.
Take for instance I have a project with folders like:
Project/src/main/java/com.website.project/Class.java
Once that I have this project packaged into a jar file, I would then place it in a folder where it would be run:
App/jars/Project.jar
I want it to access a json within the folder:
App/json/file.json
What code would I need to write to access the directory from my Class.java?
I am sorry if this was confusing, I'm not the best when it comes to Stack Overflow, but thank you so much for any help in advance!
You could keep your App/json/file.json in src/main/resources folder.
Path - src/main/resources/App/json/file.json
Then you could access it by :
JSONTokener parser = new JSONTokener(this.getClass().getResourceAsStream("/App/json/file.json"));
JSONObject jobj = new JSONObject(parser);
jobj.put("style", style[i]); // If you want to add a new key value or just replace it
The file itself will get packaged in JAR file
Don't forget to include org.json in your pom.xml dependency.
Import this in your class:
import org.json.JSONObject;
import org.json.JSONTokener;
First approach:
Please test if you can find the file with:
File file = new File("./../json/file.json");
System.out.println("File exists: " + file.exists());
Relative paths in java start with ./. When you export your jar the relative start path is the location of the jar => App/jars/ so you need to go one folder up with ../ and after that go inside /json/file.json
The disadvantage of this approach:
To work with file outside your java project (assuming your java project is in java directory) means that you have to create every time this folder structure everywhere. For example in your case if you want to work also in you IDE inside of your workspace of projects you have to add directory json and after that your file.json.
Second approach:
Another solution can be to add your file inside the java project itself. Then you will be able to read your file easy with getClass().getResourceAsStream("file.json");. Consider that file.json is inside your class's package. This way you can test and see 'file.json' also inside your IDE.
The disadvantage of this approach:
Pay attention that if you use your file inside the Java project it will end up in the jar. When this happen you no longer can access it as File. That why I am useing getResourceAsStream method in this case. To read more about this see answer:
https://stackoverflow.com/a/20389418/6068297
Also you have to know that getClass().getResourceAsStream("file.json"); will not work in static methods for example in public static main(String[] arg).
Update:
Also the jar file is meant to be archive so it must stay unchanged. So you can not (you should not) write back changes to file inside the jar. If you need to have some modifiable file then you should create it outside the jar. You can add it relative to the jar location or in a place like home directory of the current user where relevant files to the current user (who is using your application) can be created without some additional permissions.
Third approach:
You mention that you are using Maven project. There is a folder in the Maven project which is called resources => src/main/resources. This folder end up in the classpath so you can also put your file file.json there and read it as second approach with getResourceAsStream. This way you can have clear separation between java classes and other files.
The disadvantage of this approach:
The same disadvantages as in the second approach.
I hope it helps.
When I create this new class Quake and try to write some code in it ,it displays the following message,
and when I try to run it Android Studio automatically delete the class Quake.
Can someone explain me why is this happening and how to resolve it ?
You're in Packages view and putting all your files in the wrong folder. Switch to Project view (left hand, top side toggle). Under app, you should see a folder called src -> main -> java -> com.example.prateek. That's where all your class files should properly be to avoid autodeletion.
This happens due to incorrect Source folder mappings done by some mistake.
In order to resolve the same, please go to
Project Structure -> Project Settings -> Modules.
Select the Module that has this issue and select the Sources Tab
On the Right Hand Side you will find Add Content Root pane. Please Check if the Source and Test Folders are mapping to the correct root and there is no other mapping present apart from the valid root mapping [For eg src for Source Folders and tst for Test Source Folders.]
Delete any unwanted mapping present in either of them. Apply and Re sync the project.
Issue should be solved now.
I am doing some algorithmic problems on the website USACO, and for every submission they want us to make two files for input and output to test values. So if the problem was called "test", they would want users to make the files "test.in" and "test.out" which requires them to change their extensions to ".out" and ".in". How do I change their extensions from ".txt" to ".in" or ".out"?
Note: I am using Windows 8
Thank you
To make Java read files in Eclipse
One must make sure that the words typed in the code and in the name of the file match each other.
For example: The file referenced in must exist.
BufferedReader f = new BufferedReader(new FileReader("test.in"));
Also, you have to make the file under the project folder (in Eclipse or manually in Windows Explorer) because that folder is the directory at for that code, and if it is not in the folder the code will not be able to read it. You can also right-click the project to import files into the folder.
My reference: Trial and Error
http://www.coderanch.com/t/439615/java-io/java/Eclipse-won-read-text-file
I'm writing an Eclipse plugin. I'm looking for the right way to get a selected java file and find it's corresponding .class file.
The way I'm doing it now works for a standard Eclipse java project, but seems less than elegant: I call IJavaProject#getOutputLocation and combine that with the path to the selected file. Then I just change .java to .class.
Here's the code for the method that translates from the incoming selection to a path:
def selection_to_path selection
path = selection.get_paths.first
output_location = path.get_first_segment.get_output_location
root = org.eclipse.core.resources::ResourcesPlugin.getWorkspace().get_root
folder = root.get_folder output_location
folder.get_location.to_s
path_to_file_as_string = path.get_last_segment.get_path.to_s
path_to_file_as_string[/\.java$/] = ".class"
path_elements_of_java_file = path_to_file_as_string.split("/")[3..-1]
all_elements = folder.get_location.to_s.split("/") + path_elements_of_java_file
File.join(all_elements)
end
This works, but I suspect it fails often enough that it's not the right thing to do. In particular, the documentation for getOutputLocation says that my assumptions about class file locations are too simplistic.
So what's the right way to do this?
I don't need this to work all of the time. Certainly there will be many cases where a .java file doesn't have a corresponding .class file (compilation errors, no automatic builds, whatever). I don't care about those; I just want to know if there is a class file, what is its full path?
There is no method, which directly finds the corresponding class file resource. The proper way to get the corresponding output folder is to:
Convert the selection to IJavaElement, most probably ICompilationUnit (.java files)
Find the corresponding IPackageFragmentRoot (the source folder)
Find the resolved IClasspathEntry
Find the output location using IClasspathEntry.getOutputLocation() or the IJavaProject.getOutputLocation() method, depending on whether the classpath entry has separate output location.
Construct the path to the classpath based on the ICompilationUnit's package
The class file may or may not exist depending on whether the project has been build, whether there are build path errors, etc. So, there are additional heuristics, which you need to use to determine whether the result of the above algorithm will be usable.