where to put .properties files in an Eclipse project? - java

I have a very simple properties file test I am trying to get working: (the following is TestProperties.java)
package com.example.test;
import java.util.ResourceBundle;
public class TestProperties {
public static void main(String[] args) {
ResourceBundle myResources =
ResourceBundle.getBundle("TestProperties");
for (String s : myResources.keySet())
{
System.out.println(s);
}
}
}
and TestProperties.properties in the same directory:
something=this is something
something.else=this is something else
which I have also saved as TestProperties_en_US.properties
When I run TestProperties.java from Eclipse, it can't find the properties file:
java.util.MissingResourceException:
Can't find bundle for base name TestProperties, locale en_US
Am I doing something wrong?

Put it at the root level of one of your source paths, or fully qualify the resource name in the call to getBundle, e.g.
ResourceBundle myResources =
ResourceBundle.getBundle("com.example.test.TestProperties");
See the docs for ResourceBundle.getBundle(String, Locale, ClassLoader) for more information.

Do NOT put your propierties files into your src folder! Obviously that works, but basically this is NOT how you should approach your problems. Create a new folder in your project, for instance a 'Resources' folder, add it to the classpath in project properties and put all files other than .java there.

I have just been trying to solve this problem as well, I have found that you must refresh Eclipse's list of files before you try to run your project. Then you can have your files in the base directory and use them as normal.

put the TestProperties_en_US.properties(propery) file in the src folder and then run the program it will run

Aha, thanks a bunch. This also works.
package com.example.test;
import java.util.ResourceBundle;
public class TestProperties {
public static void main(String[] args) {
ResourceBundle myResources =
ResourceBundle.getBundle(TestProperties.class.getCanonicalName());
for (String s : myResources.keySet())
{
System.out.println(s);
}
}
}

Related

How to create a single jar from multiple packages?

I want to create a single jar file from multiple packages. I have created the jar using below command but when I'm importing it to a project as a dependency it is not working.
jar cfe output/jar/my-java.jar Main src/pkg1/pkg0/*.class src/pkg1/*.class src/pkg2/*.class
My project structure is something like below structure
src
pkg1
A.java
B.JAVA
pkg0
E.java
pkg2
C.java
D.java
My Example code is something like
import pkg1.A;
public class Main {
public static void main(String[] args) {
A.printMe("Hello World");
}
}
error that I'm getting is:
java pkg1 not exist
But in the editor(IntelliJ), it is not showing errors and also i'm able to import class but not package.
import pkg1: showing red means error in the editor
import pkg1.A: not showing red means no error in the editor
Note: I don't want to use maven.
An unzip -t something.jar shows the actual file structure of the jar file (zip). It is the same as the class structure of it (except that instead "/", a "." is the separator).
In your case, the problem will be that src will be on the top level, and not pkg1. Either import src.pkg1 (very dirty), or play a little bit more with the directories / jar flags.
just call your method it automatic get path if you correctly put your jar in your current project.`project 1
pkg com.test.demo
class test{
public static void m1(){
System.out.println("project 1 in method 1 );
}
}
in project 2 put jar of project 1
pkg com.test.demo
class Test1{
public static void main(String...){
System.out.println(test.m1())
}
}

Java class.getResource() returns null

I am trying to use files in resource directory that was marked as "resource root" in IntelliJ, but the below code fails to find the file.
Could you tell me what was wrong? thanks.
public class ResourceTest {
public void testResource() {
URL url = this.getClass().getResource("resources/table.1gram");
System.out.println(url);
}
public static void main(String[] args) {
ResourceTest rt = new ResourceTest();
rt.testResource();
}
}
Files in the resources folder will be packaged to the root of the .jar file, meaning that during development, the resources folder itself is in the classpath, so you need this.getClass().getResource("/table.1gram"), or without the / since your class is in the unnamed package, aka also in the root of the .jar file.

Need help setting up intellij java project with multiple .java files from scratch

Edited to restart question from scratch due to complaints. I am a newbie to this format and to intellij so please excuse...
I am building a project in intellij for class. This project imports jnetcap and uses it to process a captured pcap file. My issue is I have two class files I am trying to integrate. NetTraffic which is the user interface class, and ProcessPacket that actually reads in the packet and does the work.
I have tried to make a project and import ProcessPacket into NetPacket but have been unsuccessful so far. I am sure I am missing something simple in this process but I just can not find anything showing the proper way to do this.
I have gotten it working by making a package under the src directory and adding both files to that package. This doesn't require an import from the NetPacket class and seems to work but my worry is that I need to be able to run this from a linux command line. I have been working all semester so far with everything in one source file so it hasn't been an issue until now. I don't remember using packages in the past under eclipse to do this.
Can someone offer a step by step process on how to properly add these source files to my project so that I am able to import ProcessPacket into NetTraffic or will leaving like this in a package work fine?
The files in question reside in package named nettraffic in src directory.
NetTraffic.java
package nettraffic;
public class NetTraffic {
public static ProcessPacket pp;
public static void main (String args[]) {
pp = new ProcessPacket();
pp.PrintOut();
}
}
ProcessPacket.java
package nettraffic;
import org.jnetpcap.*;
public class ProcessPacket {
public ProcessPacket() {
}
public void PrintOut() {
System.out.println("Test");
}
}
Note there is no real functionality in these at this time. Just trying to get the class import syntax correct before continuing. Again while this seems to work as a package I want to have it done without using a package and importing ProcessPacket.java into NetTraffic.java.
public class NetTraffic {
ProcessPacket pp = new ProcessPacket();
pp.PrintOut();
}
You're calling the PrintOut() method outside of any constructor or method or similar block (static or non-static initializer blocks...), and this isn't legal. Put it in a constructor or method.
public class NetTraffic {
public NetTraffic() {
ProcessPacket pp = new ProcessPacket();
pp.PrintOut();
}
}

Need to import custom package to my class

Hi I have a custom class which consists of a custom exception folder and 3 .java files.
My structure is com/raeside/family/
So I want to do the following
import com.raeside.family.*;
But it is just giving me a red underline and when I try to let netbeans repair it, it doesn't work. Where should I store my com/ folder so that I can import it into my java program? I tried storing it inside of projectname/src/ but it's to no avail.
package com.raeside.family;
public class W4Q3 {
public static void main(String[] args) {
System.out.println("hello");
}
}
Is my code, my W4Q3 file is in the family folder. Is there something I am missing?
If all of your classes are in the same package (e.g. com.raeside.family) then you don't need the import statement at all. Make sure that all of your java files are in the src/com/raeside/family directory and have
package com.raeside.family;
at the top.

import from external lib jython

I am trying to imported a java class from an external lib in jyhon and it does not work. An example
package run;
import import.Imported;
Class Run()
{
public static void main(String[] args){
pi = new PythonInterpreter(null);
pi.execfile('script.py');
}
}
//this is an external libary
package import;
Class Imported()
{
//some stuff;
}
//py script
from import import Imported //this line throws an error Module not found
#do some stuff
The strangest thing is that it runs when it is compiled in Eclipse, but does not from command line.
Any help?
Sounds like your classpath is probably set incorrectly at runtime. The easiest solution is typically just to add the directory or jar file containing 'import' to sys.path.
(Also, naming your packages 'import' is just asking for trouble.)

Categories