Javac can't find .class files, have right classpath - java

Although I'm not new to programming in general, I am new to java, and it seems to work a little bit differently. When trying to compile a .java with javac it gives me this error: cannot find symbol. I looked the cause of this up, and the error occurs when the .class file that you are referencing could not be found. I ensured that all of my references were spelled correctly, I ensured I had the right classpath, I tried compiling a .java file I know is valid, even on an XP computer just in case my Vista installation was the cause, but to no avail. Here are the contents of the .bat I am using to run javac, it could be the cause of my problems.
#echo off
"C:\Program Files (x86)\Java\jdk1.6.0_21\bin\javac" -encoding UTF8 -cp . hl.java
pause
Thank you for the help, this has been troubling me for quite some time now.

Can you show us this?
I tried compiling a .java file I know is valid
Could it be that you are using classes not compiled on . ? Unless your code is as simple as hello world, I'd say chances are high.

This is a long shot, but looking at your script and the error message you described, you're probably not running javac at all. Try this on the command line and see what it says:
C:\Program Files (x86)\Java\jdk1.6.0_21\bin\javac -version
If that doesn't work, then you're not pointing to a valid javac.

Related

ANTLR Warning: TestRig moved to org.antlr.v4.gui.TestRig

I have been trying to get ANTLR to work all day. I have used several websites including antlr.org, numerous StackOverflow queries and the textbook by Terence Parr. I am on a Windows 7 machine (work machine, can't change to Linux) but I have tried this on both CommandPrompt and Cygwin, with the same result.
No matter what I do, when it comes time to use the grun file, I always come up with the same error in the end:
Warning: TestRig moved to org.antlr.v4.gui.TestRig; calling automatically
Can't load Hello as lexer or parser
I have the most recent versions of the JRE and JDK on my computer, as well as the most recent version of cygwin.
I can run the batch files for grun and antlr4 from anywhere, so I'm strongly guessing my path is set correctly. I can run antlr4.bat with absolutely no issue. It creates every file the text book says I should see:
Hello.g4 HelloBaseListener.java HelloLexer.tokens HelloParser.java Hello.tokens HelloLexer.java HelloListener.java
I can run javac on those files and it generates various CLASS files:
HelloBaseListener.class HelloLexer.class HelloListener.class HelloParser$RContext.class HelloParser.class
But then, when I try to run grun on it, I get this:
Warning: TestRig moved to org.antlr.v4.gui.TestRig; calling automatically
Can't load Hello as lexer or parser
I've tried several combinations of commands to use the grun file, but the one it says to use in the book is:
grun.bat Hello r -tree
I've also changed the .bat file with grun several times, just to experiment, using different combinations in the books and on the sites, but I always come back to this:
java org.antlr.v4.runtime.misc.TestRig %*
That is what my boss told me to use, and it works for him.
Use this in your grun.bat command
java org.antlr.v4.gui.TestRig %*
I had the same problem, there is actually already a thread with the solution here --> antlr4-Can't load Hello as lexer or parser
The first part of the message "Warning: TestRig moved to org.antlr.v4.gui.TestRig; calling automatically" just means that the TestRig order is saved to another location, it works anyway but I changed my batch data accordingly and did not get the Warning anymore. The second part of the message is the actual problem (see link). In my case I forgot the "." (dot) in my CLASSPATH in Windows.

Running a java program using -cp

so I've checked many posts here and tried everything people suggested and I'm still getting "Could not find or load main class" error, and my professor wasn't very helpful in his answer when I asked for help.
This is what my professor said is supposed to work (he is using OSX).
java -cp classes:lib/json.jar cst420.jsonrpc.client.GroupServerJavaStub http://127.0.0.1:8080
After reading some other posts on this site, I also tried:
java -cp classes:"lib/json.jar" cst420.jsonrpc.client.GroupServerJavaStub http://127.0.0.1:8080
java -cp "lib/json.jar" cst420.jsonrpc.client.GroupServerJavaStub http://127.0.0.1:8080
Still nothing works. I'm in the base directory in a bash prompt ~\GroupJsonRPC and the class file that is in ~\GroupJsonRPC\classes\cst420\jsonrpc\client\GroupServerJavaStub.class exists and is ready for running. The same goes for ~\GroupJsonRPC\lib\json.jar. Any insight into how to get this darn thing running would be greatly appreciated!
PS: I'm using Windows.
The proper command is:
java -cp "classes;lib/json.jar" cst420.jsonrpc.client.GroupServerJavaStub http://127.0.0.1:8080
You need to be sure to include the directory of your class files as well as the jar file(s) with ';' separators, and don't use the -jar option. It invalidates the -cp option.

Setting up Java compiler on Windows laptop

I have a laptop with a Windows 7 OS 64-bit. I'm trying to set up the Java compiler so when I type in "javac" in the command prompt, it will compile the Java file I want. I've had it done on my laptop before but someone did it for me. They went to the Environment Variables under the Advanced System settings, then edited the "PATH" so that it would recognized the compiler. So I did the exact same thing, I copied the file location of javac from ProgramFiles, and put it in PATH. It still does not compile my Java files. It says:
"javac" is not recognized as an internal or external command.
And before you ask, I did search through other forums on this site to see if there was an answer that can help. There are similar situations to mine but the solutions did not help me.
As of now, this is the file path that I tried copying into PATH:
C:\Program Files\Java\jdk1.7.0_25\bin This leads to a file marked javac. So far I'm still unsuccessful with setting up the compiler.
It looks correct. Did you restart the Command prompt every time you changed the PATH variable ? Also there's supposed to be only one path variable, so you're supposed to edit the existing one, entries are separated by a semi-colon (;).

Java - classNotFound Exception from Windows command line but not Eclipse

My code works fine in Eclipse, no build path errors or anything, however when I try to run my Java program from the command line, I get a classNotFound Exception on one of my inner classes. Not only do I not understand how I am getting this exception, but I am not even sure how to go about debugging it, since it looks and works fine in the IDE environment. Any help is appreciated!
Edit
I am writing a compiler for a subset of Java called J--, so I dont really want to get too into how it all works. But instead of calling javac HelloWorld.java I would call the equivalent j-- HelloWorld.java. javac works fine. You might say well the issue is with your code, but again it compiles and runs fine in Eclipse. So somewhere there seems to be a disconnect. Here is the Windows bash script if it helps:
set BASE_DIR=%~dp0
set j="%BASE_DIR%\..\"
set JAVA=java
set CPATH="%BASE_DIR%\..\lib\j--.jar;%BASE_DIR%\..\lib\spim.jar"
if "%CLASSPATH%" == "" goto runApp
set CPATH=%CPATH%;"%CLASSPATH%"
:runApp
%JAVA% -classpath %CPATH% jminusminus.Main "j--" %*
set JAVA=
set BASE_DIR=
set CPATH=
Edit
Thanks to Aubin, outputting the .jar file and comparing that with the class not found was how I was able to solve this conflict.
Usually Eclipse takes the sources from src and produces classes file into bin.
Try:
java -cp bin a.b.c.d.MyClass
To call your tool as j-- <args> you need to write a shell which embed the command:
java -cp bin a.b.c.d.MyClass $*

regarding javac

javac is not internal or external command error is coming. I have set the path. then also it is giving the same error.
Are you sure you installed the JDK?
The JRE (aka "Java Runtime") does not contain javac, this is only part of the JDK ("Development Kit")
You should first examine the PATH by executing this command:
echo %PATH%
Among these folders should be at least one that looks similar to this:
C:\Program Files\Java\jdk1.6.0_20\bin
Maybe you left out the bin at the end, maybe there isn't such a folder name at all in your PATH? We cannot know. If you tried this and are still unsure, post the value of the PATH so we can give you real tips instead of just guessing.
If you're on windows, and running from a command prompt you need to reopen it to force any PATH changes to take effect

Categories