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.
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.
My code definitely works. It's a very simple program. It takes the first item in String args (args) 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.
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.