-1

I have a few questions because it seems the basis of my problems is I'm not understanding how to properly run my java class files from the command prompt and have them take arguments.

I have my project folder 'ProjectDNA' and within this directory is my commands.txt that I would like my class to call as an argument.

ProjectDNA\src\package\dna\sequencer is the class I am trying to call.

My issue is I am able to compile the sequencer in a command prompt from ProjectDNA\src\package\dna, but only execute it from

ProjectDNA\src

This only brings in more confusion when the .txt file I need it to process as an arg is in ProjectDNA\commands.txt

After going through some other similar questions here. here, here, and looking at oracles documentation I'm not 100% on how I am supposed to efficiently compile,run, and take the txt file as an argument. Where should I be calling these commands and how should the syntax be in the command prompt? When I run from my src file:

java -cp . package.dna.Sequencer commands.txt

I receive

java.io.FileNotFoundException: commands.txt (The system cannot find the file specified)
        at java.io.FileInputStream.open0(Native Method)
        at java.io.FileInputStream.open(Unknown Source)
        at java.io.FileInputStream.<init>(Unknown Source)
        at java.util.Scanner.<init>(Unknown Source)
        at package.dna.Sequencer.process(Sequencer.java:55)
        at package.dna.Sequencer.main(Sequencer.java:113)

Any help would be greatly appreciated.

Community
  • 1
  • 1
Alkarin
  • 464
  • 7
  • 20
  • If you are in the `src` directory, then `commands.txt` is not in the same directory. You need to give either its full or its relative location to the current directory. – RealSkeptic Mar 13 '16 at 19:39

1 Answers1

1

You've got a couple of different concepts here. One is how Java deals with the classpath and the other is the default location that a Java program will look for a file.

When you use the java command and supply the classpath setting (-cp in your example) you are telling Java where the package(s) you want on the classpath are located. In your example you are saying that the packages are located in the current directory (the . means current directory). You then give the fully qualified class name to run (e.g. with the main() method). At this point Java starts running and loads the requested class, executing the main() method. The directory where you start the Java program is considered the current working directory.

When you create the FileInputStream and give it a file name it will look for that file in the current working directory. You can supply a fully qualified path to the file instead of just a name. You can also use a relative path. For example, in your case you could give the command as:

java -cp . package.dna.Sequencer ..\commands.txt

The .. means the parent directory above the current working directory. This would cause the FileInputStream to look for the commands.txt file in the ProjectDNA directory above the src directory (where you started the Java program).

Alternatively you could start the program from the ProjectDNA directory and use this command:

java -cp src package.dna.Sequencer commands.txt

In this case your current working directory will be ProjectDNA (containing your commands.txt file as well as the src directory). The -cp src tells Java that the src directory should be on the classpath (e.g. class files will be found in packages under that directory).

Dave
  • 28
  • 5
  • Thank you, not only does this explain whats going on but you also offered the solution, and it seems most of my confusion from coming from the '.' that I've been seeing. I appreciate your explanation and I feel its a bit more clear to me what I was doing wrong and why. – Alkarin Mar 13 '16 at 20:20