aspose file tools*
The moose likes Beginning Java and the fly likes Classpath? Duh, what? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Classpath? Duh, what?" Watch "Classpath? Duh, what?" New topic
Author

Classpath? Duh, what?

David Hulse
Greenhorn

Joined: Apr 09, 2011
Posts: 24

So I'm trying to do these challenges from Facebook. They seem easy enough but there's one part that I'm missing. I'm supposed to make my programs able to take arguments from the command line. The only problem is that I don't know how to test to see if they'll really take the arguments. I'm assuming that once they're added it would be just like running any other command from the terminal except with java in front of it


I also assume that there is a way that my jar files could be added to the list that my bash terminal uses so that I could just do this


I don't have a clue how to edit my classpath or even if that's the correct thing to do. I've tried to find configuration files and I've tried set Classpath and java -cp but nothing I do is working. I really need a step by step or a first step with a link to further instructions. Even just something to read so I'm not a total moron.

Please enlighten me.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61199
    
  66

It would be useful to let us know what platform. Assuming Windows?

You can set a CLASSPATH environment variable, but the format differs between Windows and other operating systems.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
David Hulse
Greenhorn

Joined: Apr 09, 2011
Posts: 24

I'm using Mac OS X Snow Leopard. So I was right in assuming that altering my classpath is the solution?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61199
    
  66

Yes, the classes must be on the classpath to be found at run time.

I work exclusively on web apps (where the classpath is automatically handled by the servlet container) so I'll let someone else comment on the exact format of the CLASSPATH environment variable, but I'm pretty sure you just list the classes and jar files separated by colon characters.

David Hulse
Greenhorn

Joined: Apr 09, 2011
Posts: 24

Ok. I'll wait for a response. Until then my understanding is this:

I use the java -cp command and I enter the absolute path of my .jar file?

and that will just add to my classpath? not completely screw it up?
David Hulse
Greenhorn

Joined: Apr 09, 2011
Posts: 24

My code definitely works. It's a very simple program. It takes the first item in String args[] (args[0]) and then prints it out to standard out provided of course that there is a first argument. If there is not it throws an exception.

Do I need to do something special with my packages? I've read that their may need to be a certain filesystem hierarchy that my .jar file is located in so that the classpath with be able to call my file using the package name like this: com.dave.sophie

I've also read that it doesn't really matter if you're just testing a simple throwaway program. I've been trying everything I can think of and I'm still unable to run my program using just java sophie [arguments]. I did successfully execute with java -jar sophie.jar [arguments] while I was in the correct directory.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14146
    
  18

You can set the classpath using the -cp option, but this will not add to your classpath. If you use the -cp option, Java will use whatever you specify there and it will not look at the CLASSPATH environment variable at all.

When you use the -jar option, then using the -cp option has no effect. The -jar option is used for running executable JAR files. In an executable JAR, the main class and the classpath are specified in the manifest file inside the JAR file instead of on the command line.

If your JAR file is not an executable JAR file with a manifest file, then don't use the -jar option. Use the -cp option to set the classpath to your JAR file (and other directories or JAR files if necessary) and specify the name of the main class on the command line. For example:


# For running an executable JAR (classpath and main class specified in the manifest file):
java -jar MyJarFile.jar

# For running a program packaged in a JAR that doesn't have a manifest:
java -cp MyJarFile.jar org.mypackage.MyProgram

The directory of your source code files and compiled classes, also inside the JAR file, must match the package structure. If your classes are in a package called com.dave.sophie, then you must have the files in a directory structure com\dave\sophie.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Classpath? Duh, what?