• Post Reply Bookmark Topic Watch Topic
  • New Topic

Running foo.java from anywhere  RSS feed

 
Jim Venolia
Ranch Hand
Posts: 312
2
Chrome Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I knew the correct terms google would be nicer to me

I've got Foo.java running nicely, and now I want to put it in a common bin directory so I can just run it. How do I do it?

That is, I want to do something like:

javac Foo.java
cp Foo.class /usr/local/bin
rehash
cd ~/data
java Foo data.txt
cd ~/different/data
java Foo nudata.txt

I've seen a couple different errors, but the one I see most often is "Could not find or load main class"

Be nice if I could lose the java and just type 'Foo data.txt'
 
E Armitage
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Running a Java program requires the classpath to be pointing to the location that has the required class files including the class that contains the main method. If you want to run the program from any directory in your system then you need to use absolute paths to point to both the class that you are trying to run and its class path. If you then want to shorten the command typed by the user then you can create an alias using your operating system's commands that runs the longer command or create a bash script that calls the full command.
 
Jaikiran Pai
Sheriff
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To add to what E Armitage said, here's a FAQ which explains the basics of classpath http://www.coderanch.com/how-to/java/HowToSetTheClasspath
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!