Link to home
Create AccountLog in
Avatar of wsyy
wsyy

asked on

Java Singlton web file path

Hi,

My web application needs to call a java class, which is a singlton and load files as inputs.

The structure is as follow:

ProjectName
    src/...../YYTLinearClassifier.java
    training/test.txt
    WebContent/index.jsp

The Test.java file is a singlton, and called by the index.jsp file.

During the initialization of the instance of the Test.java, the initialization will load the test.txt (under the training folder). Part of the .java file is attached.

I used a number of way to load the .txt, but failed to do so.

Does anyone have the same experience?

Thanks


public class YYTLinearClassifier extends ColumnDataClassifier {

	private static Classifier<String, String> cdc;

	private static boolean serialized = true;

	private static final String modelPath = "/training/model/yytmodel.ser";
	private static final String propPath = "/training/yytmodel.prop";
	private static final String trainPath = "/training/yytmodel.train";

	private static YYTLinearClassifier instance;

	public static YYTLinearClassifier getInstance() {
		if (instance == null) {
			Properties props = StringUtils.propFileToProperties(propPath);
			instance = new YYTLinearClassifier(props);

			if (!serialized) {
				cdc = instance.makeClassifier(instance
						.readTrainingExamples(trainPath));
				serialized = serializeClassifier(cdc);

			} else if (!deserializeClassifier()) {
				instance = null;
			}
		}

		return instance;
	}

Open in new window

Avatar of for_yan
for_yan
Flag of United States of America image

If this is  web application - you classes should be in WEB_INF/classes folrder

If you what to load .properties file it can be done with the

Properties.load (FileReader) method

http://download.oracle.com/javase/6/docs/api/java/util/Properties.html#load%28java.io.Reader%29
This is how you can load propries in the web application:

ClassLoader classLoader = Thread.currentThread().getContextClassLoader();

Properties.load(classLoader.getResourceAsStream("/file.properties"));

Your file file.properties should be in the WRB-INF/classes  folder

see:

http://stackoverflow.com/questions/3160691/how-to-read-properties-file-in-web-application


Avatar of wsyy
wsyy

ASKER

CEHJ,

The files are loaded in a singlton class not a servlet. So I don't know how the getRealPath() method works.
Avatar of wsyy

ASKER

for_yan,

What class files should I put into the classes folder?

I have the .java file run successfully as application, and there is no classes folder under Web-INF folder.
if you run it as applcications (not WEB)

then just uise in your code

Properties.load(this.getClass().getClassLoader().getResourceAsStream("file.propeerties"));

and you file should be in the top level of your classpath
Avatar of wsyy

ASKER

Unfortunately, this is a web application.
If this is a Web application then the top of your classpath in WEB-INF/classes
Avatar of wsyy

ASKER

Maybe I didn't explain clearly the problem.

I need to know the location of the .java file compared with the Web-INF folder.

The .java file is not in the Web-INF folder, nor the Web-INF/classess folder
Avatar of wsyy

ASKER

the relative location of the .java file.

Can I know its relative location when I run the .java file?
check this,but I'm not sure it will work:
http://www.exampledepot.com/egs/java.lang/ClassOrigin.html

// Get the location of this class
Class cls = this.getClass();
ProtectionDomain pDomain = cls.getProtectionDomain();
CodeSource cSource = pDomain.getCodeSource();
URL loc = cSource.getLocation();  // file:/c:/almanac14/examples/

why do you  need to determione location for your class?

if it is web application and your class is not in a jar file
the it would be located in WEB-INF/classes/its_pacakge_path
so you should be able to detrmine it from the full name of the class
which can be found with reflection, I guess,

But why would you need all that?


Avatar of wsyy

ASKER

if it is web application and your class is not in a jar file
the it would be located in WEB-INF/classes/its_pacakge_path


for_yan, I see no files in the Web-INF/classes folder.
What engine/ appserver are you using - is it Tomcat or something?
How do you execute your application?
If your ultimate point is to load .properties file - you can load it specifying the absolute path to the .properties file not worrying about whether it is web or non_web application
Avatar of wsyy

ASKER

The reasons why I want to know the location of the .java file are:

1. I don't know where the compiled classes are located. If they are under Web-INF folder, then things are relatively easy.

2. I want to use that information to determine the relative path of the training folder to the .java file.
Avatar of wsyy

ASKER

I am using Tomcat, and I run the index.jsp first by submitting an input string for the java file to execute.
Avatar of wsyy

ASKER

I am currently run the web application in Eclipse.
Submitting input striung to java file - what that means?
You either start it with java command on the terminal or if you are using index.jsp - that means you are opening it with a browser.
So are you running it pointing a browser to index.jsp ?
Avatar of wsyy

ASKER

Another question is that the test.txt file is not a property file.

Can I use the property method to load it? How can I parse the text file or read the contents of the file?
If you put your properties file in the /src foder of your project and the use thise getResourceAsStream options we discussed earloer then eclipse should rtake care of the file and package it into war or whateveR and it shoul work
Avatar of wsyy

ASKER

Yes, I was running the index.jsp in Eclipse web browser.
I think you can use txt file but its contents should be in properties format. If it is in propeerties format better name it .properties
>>The reasons why I want to know the location of the .java file are:

The location of the .java file is not relevant - knowing it won't help you.

It's where the class file of this singleton is placed that's relevant. If you want it to run in conjunction with a web app, then you must place it, if in a jar. under WEB-INF/lib. If it's not in a jar, then in WEB-INF/classes

The other issue is whether you need to edit this file (test.txt) after the app has been deployed - do you?
Avatar of wsyy

ASKER

I ran the line of code in the java file:

System.out.println(YYTLinearClassifier.class.getProtectionDomain()
                        .getCodeSource().getLocation());

The output is:

file:/C:/workspace/dev1.0/YYTClassifier/build/classes/
Avatar of wsyy

ASKER

No I don't have to modify the .txt file but read its contents within the java file.
If you're working with Eclipse, i suggest you do the following:

1. create a new package called 'resources' under your package root
2. put the file(s) you want to read in there
3. load the resource(s) with the code below or variants thereof

It should then work both in the IDE and afterwards, when deployed
InputStream in = getClass().getResourceAsStream("/resources/text.txt");

Open in new window

Avatar of wsyy

ASKER

CEHJ,

Your latest suggestion seems a little bit easier to me. But "1. create a new package called 'resources' under your package root"

What does the package root exactly mean?

The project root folder contains such things as build, .settings, src, WebContent, etc.

Do you mean the resources package is created in the project root, or one level below?

'src' is really the package root, so you'd create a new folder directly under that
Avatar of wsyy

ASKER

Hi guys,

While asking ur suggestions, I try the following:

1. place the .txt file under WEB-INF/classess

2. modify the java file with

private final static String relative = YYTLinearClassifier.class
                  .getProtectionDomain().getCodeSource().getLocation().getPath();

      private static String modelPath = relative + "test.txt";

The code then is working when I ran it in Eclipse as application. But it doesn't when I ran it in Eclipse browser. The exception is this:

could not read properties file: /C:/workspace/dev1.0/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/GuessCategory/WEB-INF/classes/com/yyt/core/classifier/YYTLinearClassifier.class/test.txt

Why does this happen?

I think it is pretty solid that I find out the WEB-INF\classes folder, and I know the .txt is inside. How can't it work properly?

Will this work if not in Eclipse but in Tomcat?
>>I think it is pretty solid that I find out the WEB-INF\classes folder, and I know the .txt is inside. How can't it work properly?

The simple answer to that is that it will be erased as soon as Eclipse cleans/rebuilds. I'm not sure why you ignored my last suggestion...
Avatar of wsyy

ASKER

CEHJ,

I do not ignore but would try every means to figure it out. Will definitely try that.
It's the only way you'll have it working both in the IDE and out of it
That's what I was suggesting all along:

see my reamrak in 37093255

If you put your properties file in the /src foder of your project and the use thise getResourceAsStream options we discussed earloer

You may create resources folder in there or you may not or may create any other folder
but the important thing is to put your file .txt or .properties in the /src folder and the use

this.getClass().getResourceAsStream("text.txt") - to make an input stream form the contents of this file - in this case it should be directly in the src - you need not to deal with any packages
and it should work both in EClipse and later on when you make war, or a jar out of it
 
>>If you put your properties file in the /src foder of your project ....

That's not what's required. What's required is to create a new package and place the resource(s) in it
why do you need a new package?
it will work without resourse/ there also, just sitting in the root
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of wsyy

ASKER

Guys, great discussions!

Much more clear now for beginners like me.
:)