2

So I want to run a class called WordFrequencyCounter which is nested in the folder: C:\Users\Mike\Documents\Computer Science\AI\Machine Learning\175\Assignment 2\src\ir\assignments\two\b

but when I go into that folder and run:

javac WordFrequencyCounter.java

it gives me an error because it says that it cannot find import ir.assignments.two.a.Frequency; specifically it says package ir.assignments.two.a does not exist, but this file is located in:

C:\Users\Mike\Documents\Computer Science\AI\Machine Learning\175\Assignment 2\src\ir\assignments\two\a

I also tried calling javac WordFrequencyCounter.java from within the folder: C:\Users\Mike\Documents\Computer Science\AI\Machine Learning\175\Assignment 2

but that does not work either. So what I want is to be able to compile and run WordFrequencyCounter and then feed it an input file that is located in C:\Users\Mike\Documents\Computer Science\AI\Machine Learning\175\Assignment 2 from the command line. I can do this from the IDE but can't get it to work through command line.

I think this is a classpath related problem but I couldn't find anything useful on this for my particular example, and nothing I tried worked. enter image description here

Here's the main method for WordFrequencyCounter:

public static void main(String[] args) throws FileNotFoundException {
    File file = new File(args[0]);

    List<String> words = Utilities.tokenizeFile(file);

    List<Frequency> frequencies = computeWordFrequencies(words);
    Utilities.printFrequencies(frequencies);
}
1
  • Is WordFrequencyCounter in package ir.assignments.two.b ? Commented Nov 27, 2012 at 6:06

1 Answer 1

2

go to src folder and run

javac -d . ir/assignments/2/b/MainClass.java

and use java ir.assignments.2.b.MainClass to run it.

Sign up to request clarification or add additional context in comments.

3 Comments

Great the compilation worked. But I'm still stuck running code because I have an input file in the src's parent folder (Project folder). I added a picture of the file structure for clarification. Also, I was wondering how to do it by setting the CLASSPATH environment variable?
If it is a ClassPathResource run your code using java -cp ..;. ir.addignmanets.2.b.MainClass. This will add parent lib and current lib to jvm class path.
try java -cp ..;. ir.addignmanets.2.b.MainClass ../inputfilename This will pass the file path(relative path ) to java main

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.