Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to calling a class from another folder

 
druhy svet
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greetings all,

I am very new to Java...starting out with basics.
So not using any IDE, just plain Notepad and compiling and running from command line.

I was trying to call a method of a class in another folder. But getting an error.
Placing the codes below.

Folder 1: H:\J\A\
File: Test.java




Compiled this.
H:\J\A>javac Test.java

No errors.


Second Java file which contains 'Main' and calls the 'Test' class.

Folder 2: H:\J\B\
File: UseTest.java



When trying to compile this -
H:\J\B>javac -classpath H:\J\A\; H:\J\B\UseTest.java

compiled fine.

But when trying to run this-
H:\J\B>java -classpath H:\J\A\; H:\J\B\UseTest

got error-
Error: Could not find or load main class H:\J\B\UseTest

Can somebody help me with this?

Thanks in advance.

~DS

edit: added code tags /SL
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch.

If you keep classes in separate directory trees, then you need to add all of them to the classpath, like java -classpath H:\J\A\;H:\J\B UseTest. You can't specify any directory as part of the class name.

The usual approach to keeping source and class files in separate directories, but still have only a single directory tree, involves packages: http://docs.oracle.com/javase/tutorial/java/package/index.html
 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch!

I went and added code tags to your post so the code is easier to read. Please see UseCodeTags to see how.
 
druhy svet
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for clarifying.
It is working fine now.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic