Java - classNotFound Exception from Windows command line but not Eclipse - java

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 $*

Related

Where should I put -cp when compiling/running java files

I tried
javac Something.java -cp ".; some\\directory\\xxx.jar"
and it worked. I could compile the file in this way.
But when running the file, i realized that I must put -cp in front, making it
java -cp ".;some\\directory\\xxx.jar" Something
Why is there such a difference?
Thanks
while running, anything you provide after Something will be treated as program argument

Run Java Project From Linux Cmd

I've written a java program and I'm trying to run it from cmd both in Windows and Linux.
I first compiled and then run it , and it worked just fine on Windows.
The problem comes up when I'm trying to do the same thing on Linux. The commands I've used on Linux are :
javac -cp aspose-cells.jar:aspose-words.jar:aspose-slides.jar ConvertToPdf.java
java -cp aspose-cells.jar:aspose-words.jar:aspose-slides.jar ConvertToPdf
The first command , which starts with javac, works just fine and outputs the ConvertToPdf.class file. The problem comes up after running the second one , which outputs the following error:
Error: Could not find or load main class ConvertToPdf
I'd like to know what am I doing wrong , and I'd be grateful if anyone could point me to the solution.
Thanks in advance.
Add the current directory . to the classpath:
java -cp aspose-cells.jar:aspose-words.jar:aspose-slides.jar:. ConvertToPdf
your working directory is not included in class path.
do this :
export CLASSPATH=$CLASSPATH:<your-jars>:.
it should work !!!

Could not find main class when double clicking jar- tried quite a few solutions

I searched through other posts related to this problem but haven't been able to find a solution that fixes my problem.
I can use java -jar helloworld.jar via command prompt and it runs perfectly, however if I double click on it command prompt briefly opens and I glimpse the error in the title.
One person offered a solution in the form of going into the registry and checking HKEY_LOCAL_MACHINE\SOFTWARE\Classes\jarfile\Shell\Open\Command value is set properly to javaw file pathway, which it as:
"C:\Program Files\Java\jdk1.8.0_05\bin\javaw.exe" -jar "%1" %*
Another solution offered reference to having in PATH Environment Variable, I have this in there:
C:\Program Files\Java\jdk1.8.0_05\bin
I am certain the MANIFEST file is correct, as the project runs fine in Netbeans and of course when you try Command Prompt java -jar helloworld.jar it works perfectly.
Can anyone advise? I have been fiddling with this for hours to no avail and I can't determine where I am going wrong. Thanks stackoverflow!
EDIT:
I have also tried:
assoc .jar
and
ftype jarfile
which return
.jar=jarfile
and
jarfile="C:\Program FIles\Java\jdk1.8.0_05\bin\javaw.exe" -jar "%1" %*
respectively.
here is the manifest information:
Manifest-Version: 1.0 Ant-Version: Apache Ant 1.9.2 Created-By:
1.8.0_05-b13 (Oracle Corporation) Class-Path: X-COMMENT: Main-Class will be added automatically by build Main-Class: mypackage.HelloWorld
If you are using Eclipse, while exporting Jar it asks for Main class. There you have to select a Main class from multiple classes.
From command line -
java -cp name.jar classname
here the classname should be a fully qualified class name.
My guess is that your JAR file works fine.
If you want to see the return values when executing the program, it is better to redirect the input and output to a small Swing Window. Or you would make the console wait for some input.
I ran into a problem a while ago (running IPConfig) where the command would run and the command prompt would close before I could see anything. It sounds like this is what's happening here, as far as the window going away before you can see the error.
Try opening command prompt first and running the file without the "java -jar" command and switch. As I understand things, double-clicking the .jar will try exactly that (running the file without the java command), error out, and close the command prompt window. Maybe trying this will keep the command prompt open and allow you to see the error message. It may also describe the problem (double-clicking doesn't use the "java -jar" command as far as I know).

changes to java program not being reflected

#!/bin/ksh
echo "Some Programme v1.0.0"
JAVA_HOME=/apps/clear/jdk1.7.0_45
PATH=${JAVA_HOME}/bin:${JAVA_HOME}/lib:/usr/local/bin:/bin:/usr/bin:.:
export NEWAPI_DIR=/local/newapi/1.1.1.1.2
LIBRARY_PATH=$NEWAPI_DIR/Linux-2.6/lib
MY_HOME=/home/clear/dev/app/lse
export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:$LIBRARY_PATH
MY_CLASSPATH=${MY_HOME}/lib/yt500.jar:${JAVA_HOME}/jre/lib/rt.jar:${MY_HOME}/lib/ftlse.jar:$LIBRARY_PATH/JNewApi.jar:$LIBRARY_PATH/Jfib.jar
date
$JAVA_HOME/bin/java -version
$JAVA_HOME/bin/java -classpath $MY_CLASSPATH com.company.ft.lse.LseParser /home/clear/dev/app/lse/config/config.xml
date
The above is my run script. I have an application LseParser.java to which I have changed the code. But this code is not reflected when I have run the code (. ./run) in unix box. When I locate the ftlse.jar within where the LseParser.class exists, it is last date modified some long time ago. Can someone please point me what i'm doing wrong or suggest any checks or alterations I should make ? am i missing any lines in my script i should have ?
Java is a compiled language. You need to run the javac compiler first and then the jar command to create the jar file. You can use the tutorials I linked to to figure out how to run them correctly for your project. Once you have your compile and package commands you can add them to your run script. However, I strongly recommend you look into a build automation tool such as Maven so that you don't have to work with custom built run scripts.
I think you missed export before JAVA_HOME in 3rd line... so the shell, instead of taking $JAVA_HOME it as a variable, interpret it as a command.

Cannot find class in classpath

I've been reading up on this error but am now confused. I'm trying to run my java test file from the win7 cmd line but am getting that dreaded error cannot find class in classpath.
The script runs fine as a testNG file inside the Eclipse IDE but not from the cmd line.
My classpath dump:
C:\lib\selenium-java-2.37.0.jar;
C:\lib\selenium-server-standalone-2.37.0.jar;
C:\EclipseIDE\ProteinBar\bin;
C:\EclipseIDE\ProteinBar\src\com\proteinbar\staging;
C:\TestNG;
My cmd line test string:
java -cp C:\lib\*;C:\EclipseIDE\ProteinBar\bin org.testng.TestNG DeleteBillingAddress.xml
Thanks for any help...
it doesn't appear that the testng jars are on your classpath. I'm guessing they live under c:/testng so you'll want to add something to your -cp reflecting that.
You need to specify the jars individually, e.g.,
java -cp C:\lib\selenium-java-2.37.0.jar;C:\lib\selenium-server-standalone-2.37.0.jar;...

Categories