Issues with taking args in command prompt and fileNotFoundExceptions - java

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.

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).

Related

can't run java .class file because it can't be found

Sorry, this is probably a dumb question but I am trying to run a .class file from another directory (The .class file is in ./target/classes/). When I run from the dir that the .class file is in, it runs, takes the arg, and plays the sound file just fine. When I run it from the other directory with the path to the .class file, it says it can't be found. It also turns the / into ..
I did some research and found it could be part of the CLASS_PATH variable. I added the current dir to the class path, and then tried running it. Same thing. I tried running it with the -cp ./ arg. Same thing.
I have narrowed it down to that I need to supply the path with the dots. I looked up some examples and all of them said to do something like this: com.example.foo. How would I turn this ./target/classes/SoundHandler.class (the full path is /Users/Milo/Developer/JOE/target/classes/SoundHandler.class) into that odd "special format"? Thanks!
I assume your SoundHandler.java file has no package statement whatsoever?
In that case:
java -cp /full/path/to/your/project/dir/target/classes SoundHandler
The 'special format' you're talking about is simply the full name of the class, package and all. As you have no package, the 'full name' is just SoundHandler, but you do need to tell the VM where it can find that file, which is what that -cp param will do.

jar not reading input file directory recursively

I'm having trouble running the jar command in cygwin. The input-files parameter isn't treating the directory I'm passing it recursively when I'm referencing it with ".."s in my path.
For example, I'm running this in the same directory as the "src" directory. src/ contains my package structure of class and java files. This runs properly and creates a jar containing my source and class files.
jar cf jarname.jar src
However when I run this next command, I get an empty jar except for a manifest file.
jar cf jarname.jar localdir/../src
I need to run this from a script that needs to find this directory with a ".." directory so I need the 2nd command to work.
Anybody know why this isn't working or have a workaround? I tried using realpath but it complains that it can't find that path at all. I may be using it wrong though.
The Directory path in cygwin is different . To navigate to any drive for example to C drive we need to type in:
/cygdrive/c
A very easy work around i found useful is just to type cmd in the terminal . This allows you to use the actual path than the cygwin specific path .
try typing cmd and then running the command , it worked for me

Eclimd: Can't locate files when run from vim

I have an Eclipse project:
myProject/
|--src/
| |--MyClass.java
|
|--bin/
|--MyClass.class
|--data.dat
MyClass reads from data.dat. When I edit MyClass.java and run using :Java from within vim, I get a bunch of errors:
java.io.FileNotFoundException: data.dat (No such file or directory)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:120)
at java.io.FileInputStream.<init>(FileInputStream.java:79)
If I navigate to myProject/bin and run the classes there using java MyClass, I'm able to read the data.dat file.
Where do I set the location of the files I'm trying to open so that they're relative to the compiled Java, not relative to my *.java files? It doesn't seem to be in the .classpath file.
As a secondary question: I'm having issues finding the answer to this because I don't quite know what to search for. java runtime file locations doesn't work. What keywords describe this problem so I can find the answer for myself?
This might be because the default path isn't the bin folder but the project folder itself. Try to move your file to the project folder or change its path within the program.
You could also pass the file via commandline arguments but this can cause some other problems as you might already know.

java command line argument to make jar find files in two directories?

I have a jar file which I do not have the source code but want to use.
The jar file prompts for a file to read and generates an output file using a combinatin of the input file and a number of 'helper' files it uses for data. It works perfecty fine if run from its expected home directory, but I'm trying to write a script which will allow running the jar from anywhere.
The problem is that if I try running the jar file from anywhere other then its home directories it fails to find the support files it needs to properly generate its data.
If I run the file from its expected home directory I have to give the full address of the input file or it won't find it. I would prefer to be able to give just the relative path and Java know to look at whatever directory the person calling my script is in.
Is there a way I can have a bash script pass a command line argument to Java that would ensure that this jar looks at both of the relevant directories (directory of the helper files and the current dir of the person calling the script) when trying to resolve a relative file path? Something like the -classpath argument?
With the --classpath (or -cp) you can tell your Java program where it should take the dependency classes. So, probably if you do like in your files directory
$JAVA_HOME/bin/java -cp '.:/path/to/the/original/program' My.class myfile.txt
then it will wind the program, and find your files as well.
UPDATE
If it doesn't work, you can try to force the file loading some other way. The Javadoc says:
By default the classes in the java.io package always
resolve relative pathnames against the current user directory. This
directory is named by the system property user.dir, and
is typically the directory in which the Java virtual machine was
invoked.
So, you can try running the program from the original directory this way:
$JAVA_HOME/bin/java -Duser.dir=/path/to/the/files/directory My.class myfile.txt
UPDATE2:
As I wrote in a comment, you can try symlinks. Execute the following commands in the original directory:
ln -s /path/to/the/files/directory datafiles
$JAVA_HOME/bin/java My.class datafiles/myfile.txt
Sorry - ignore. I missed the first line of your question.
You could pass the two paths as an argument to the jar file - then append the path location at runtime. Many ways to do that, here is one:
java -DdirectoryA="/somewhere" -DdirectoryB="/elsewhere" -jar program.jar
and in your code
String pathA = System.getProperty("directoryA");

Again "wrong name" error when executing java program

With reference to this post
Receiving "wrong name" NoClassDefFoundError when executing a Java program from the command-line
I did not understand how to solve the problem
Actually in my java source code there' s line :
package es_2011;
when I compile the program through JCreator everything works perfectly.
It creates a folder named es_2011 where to put .class files.
Also the executing operation goes smoothly, the program runs ok.
Now I'd like to use the command line only.
So I placed my java file in the directory where javac.exe is but whenever I try to compile I get the same error
The command I use is: javac ProgAudioJ.java
The path (where javac.exe is ) is : C:\Program files\Java\jdk1.6.0_22\bin
Is someone willing to help me understand in terms of exactly tell me what I have to do?
thanks very much...MAX
The setup used for the looks like this (under windows)
C:\classDir -> is the project
C:\classDir\testpackage -> is the only package used (the package "testpackage")
C:\classDir\testpackage\Main.class -> is the class with the main method inside (important: it is the .class and not .java)
The Main.class looks like following:
package testpackage;
public class Main {
public static void main(String[] args) {
System.out.println("Program started! ;-)");
}
}
go with your command prompt to:
c:\classDir> java testpackage.Main
the result:
Program started! ;-)
According to your problems that it starts in your IDE but not from the console:
- checked if you realy use the path to the .class files?
- with the console go to the directory of you .class files, not the project (e.g. in Eclipse it is the bin directory
- enter the full qualified class name (including packages seperated by . -> e.g. testpackage.Main
More infos can be found under:
http://www.oracle.com/technetwork/java/compile-136656.html
Hope it helped
MAX, if the class defines that it's inside the package es_2011, then it should be in a folder with the same name.
So in your case, put the ProgAudioJ.java in the folder es_2011 and then run
javac es_2011\ProgAudioJ.java
latter to run it, you need the command
java es_2011.ProgAudioJ
You should add javac.exe in your path .Edit your path variable and append path to jdk's bin
then put java file in a dir named es_2011 , as the package declaration is es_2011 then compile
c:\es_2011\javac YourJava.java
and now go back to C:
c:\java es_2001.Yourjava
After reading you other Post: "Receiving "wrong name" NoClassDefFoundError when executing a Java program from the command-line" I guess you go to the directory es_2011 where your ProgAudioJ.class file is located and run
java ProgAudioJ
from that folder.
instaend you have to go to the folder above (cd ..) and run
java es_2011.ProgAudioJ
Each package in Java corresponds to a folder on the filesystem. So a package declaration such as com.stackoverflow would mean that the source classes need to be in a folder ./com/stackoverflow. Typically the whole project would have a separate src folder containing com/stackoverflow.
When you compile the Java classes you DO NOT need to put source files in the same directory as javac.exe, you do however need to make sure that javac.exe is in your operating systems PATH variable. This tells the operating system where it should look for executable files when a command is run, on a *nix machine this would usually be /usr/bin or just /bin but on Windows machine the executables normally live within the applications own directories, that is C:\Program Files\something. Assuming that you've installed JDK correctly, the javac.exe should already be in the PATH you can check this by opening the command line and just running javac (just like that). If you get some output then all is well, the system knows where to find javac.exe.
Next you will need to go to your project folder and type javac -d . src/com/stackoverflow/MainSO.java notice that is run from the project folder. This will create a folder called com in your project root and put the compiled classes in com/stackoverflow. The -d flag tells javac where to put the compiled classes, if you leave that out, the compiled classes will be where the sources are.
Then when you want to run the classes you type java com.stackoverflow.MainSO (no .class). Crucially this command will need to be ran in the directory that contains the root of the class hierarchy (that is the com folder containing the compiled classes). You can specify other places for java to look for the classes by providing a classpath to the java command with the -cp flag. By default the classpath will contain the directory the java command was ran in. Should your project have dependencies external .jar files for example you will need to provide every single one of them (with their full filepath) in the classpath (this goes for the compiler as well). The IDEs do this automatically for you.
Hope that helps.

Categories