can't find main class java program with two jar files? - java

This is a pretty basic java question but I'm very new to java. I'm trying to follow this guide:
http://www.egtry.com/java/database/jdbc/hello_teradata
my jar files are located in:
tdgssconfig.jar located in /prod/user1/home/tdjar
terajdbc4.jar located in /prod/user1/home/tdjar
I created a file called tdTst.java in /prod/user1/home/tdjar dir with the script in the link above.
I than ran
java -cp ./:terajdbc4.jar tdTst.java
as well as
java -classpath terajdbc4.jar:. tdTst.java
but keep getting this error:
Error: Could not find or load main class tdTst.java
I'm new to java here so what am I doing wrong here? Do I need to point it to both jar files?

Putting the comment as a separate answer as suggested by #rkosegi
Use java -classpath terajdbc4.jar:. tdTst , without the .java

Related

Use class path properly in Java in command line

I'm newbie to Java. I have read all the documentations regarding specifing the classpath. But I'm still confused about my case. I'm trying to use the BuildIndex command that is part of semantic package, specifically this example,
java pitt.search.semanticvectors.BuildIndex -luceneindexpath $INDEX_MADE_ABOVE.
in here
The source of how to use the class is here https://github.com/semanticvectors/semanticvectors/wiki/InstallationInstructions#to-build-and-search-a-model
I'm trying to specify the classpath like:
java cp- {classpath} pitt.search.semanticvectors.BuildIndex -luceneindexpath $INDEX_MADE_ABOVE.
in here
But I'm not sure what the class path should be here. . The command line should have a class path, but thinking it should not be related to my project, it's part of the semantic vectors package. Do I need to clone that? its source code from the package here https://github.com/semanticvectors/semanticvectors/blob/master/src/main/java/pitt/search/semanticvectors/BuildIndex.java..
My trials was using the path of my project as the picture but didn't work. Another trial was using -jar jarName ,, got the same error: Could not find or load main class pitt.search.semanticvectors.BuildIndex. I appreciate the help as I'm confused and new to this.
In Java, classpath is the path pointing to either the directory or the jar file where your compiled java class files are located.
In your project, the class pitt.search.semanticvectors.BuildIndex is located in the jar file C:\Users\{yourusername}\Downloads\semanticvectors-5.8.jar. Therefore, the classpath is C:\Users\{yourusername}\Downloads\semanticvectors-5.8.jar.
Try something like
java -cp C:\Users\{yourusername}\Downloads\semanticvectors-5.8.jar pitt.search.semanticvectors.BuildIndex -luceneindexpath $INDEX_MADE_ABOVE
I think you mistyped cp- instead of -cp.
For more details how to use classpath, please refer to Java SE Documentation

Could not find or load main class [location]

When I try to open a java file I get an error :
could not find or load main class + location of the file
I'll give an example , let's say I have a file.java in my C:\Program Files\Folder.
When i'll try to open that file it'll say :
Could not find or load main class C:\Program Files\Folder\File.java
I looked all around google and I found things about environmental variables , but I have already set my classpath and path.
MIGHT BE IMPORTANT : I use Jdk 1.8.0_25
.java files are not meant to be launched with java command.
You should compile it first with javac that will produce a .class file then if it's a class with a main method you can use java command.
Since you seem completely lost I would suggest reading at least the java documentation and do a welcome tutorial.
Note that if as you stated in your other question (which should be closed now) that you want to run a game server, just download already compiled classes not the sources.

call java program which includes jna from batch file

I am trying to call a java program in a batch file. My java program calls a dll with JNA. When running batch file, it says it cannot find jna library class. I have already put jna jar files to my batch file folder. What can be the missing point?
My batch file:
#ECHO OFF
java -cp .;jna-4.1.0.jar com/sun/jna/Library
java MyBenchmark
I am getting below error:
java.lang.NoClassDefFoundError: com/sun/jna/Library
My jna jar file also same folder with my benchmark batch file too.
When use java -cp .;jna-4.1.0.jar com/sun/jna/Library, cmd gives that error also:
Main method not found in class com.sun.jna.Library
Library is an interface and my called java program uses that interface. But cmd says it cannot load it without main. I have to use it for reaching jna.
You must specify the ClassPath "-cp": java -cp [path to your jar] [class file to run]
Check this
If you use JNA and want to call a java program which use its library from batch file, you must avoid using java file. Because it can not load classes at runtime when you call jna.jar from batch too.
My code was like:
#ECHO OFF
java -jar jna-4.1.0.jar
java MyBenchmark
I tried many other solution too but nothing works(I also used code at question part too).
Lastly I tried export my project as jar and used it. First of all I want to state that my IDE was Intellij IDEA. It cannot put project dependency to my manifest file(jna path) although I put jar dependency already. So if you use IDEA, you must enter that from artifact screen manually, otherwise your jar cannot work right. Then you can put your jar under same folder with your bacth and call it like below:
java -jar MyJarFile.jar

Mac Terminal: Could not find or load main class CLASSNAME

I am trying to run a java program through the Terminal on Mac, yet getting:
Error: Could not find or load main class (MY CLASSNAME)
I compiled this application with Eclipse, and when I run this with Eclipse, it works fine.
Furthermore, I am in the right directory, as when I type "ls" in the Terminal, it lists all the files, includes the class file I am trying to run.
This is what I type:
java mainClass
I would very much appreciate help to solve this!
Thank you,
Dean
EDIT: Solution - instead of java mainClass, it must have package too: java startPackage.mainClass
Start by making sure you are at the directory above the top level package
If the class belongs to the package com.foo.bar, you want to be in the directory above com.
In your case, you want to be in the directory above startPack.
Then you need to use the fully qualified name to run the class...
java statPack.mainClass
For example...
Make sure you have the current directory inside your CLASSPATH.
java -cp . mainClass
To set this globally, you can use export CLASSPATH=$CLASSPATH:. inside .bash_profile.
Separately, if your class lives inside a package such as com.foo.bar, then you will need to go to the parent directory of com and run your application with the full path.
java com.foo.bar.mainClass
I too faced this on Mac machine and then what I had to do to make it work was:
Problem Statement:
I had one package xyz under the root of project i.e src/main/java and then inside xyz package I had one class Student.java
my current directory is /Users/username/projectname/src/main/java/xyz:
I can see Student.java exists here
and I compiled it using javac Student.java
Now I see class file has been created at this location. But when I try to run the class file using java Student
I get the error: Error: Could not find or load main class Student
Solution:
Now the solution is to go one step back in the directory and go to root path:/Users/username/projectname/src/main/java and run the command
java xyz.Student
and it will work.
Link to follow: https://javarevisited.blogspot.com/2015/04/error-could-not-find-or-load-main-class-helloworld-java.html
For people dumb like me, make sure you are typing java HelloWorld - and NOT java HelloWorld.class - to run the compiled file with the name HelloWorld.class. This is especially so if you are used to hitting the tab key to complete the file name, as the terminal will give you java HelloWorld.class if you hit the tab key for autocomplete after typing something like java He...
This answer is here because it took 3 sites, including this answer, and 25 mintues before I figured out what I was doing wrong.
Logic is easy, typing is hard.
Using the absolute path can also resolve this problem:
java -classpath /Users/xingliu/IdeaProjects/springproject/src/main/java/ startPackage.mainClass

java compiling error "could not find or load main class main.java"

I understand there are a lot of threads similar to this one, but I couldn't find the one that solved my problem. Following this instruction I was able to get java in terminal and be able to compile. I am able to "javac main.java" with no errors, but when I "java main.java", it simply says it could not find or load main class main.java. I believe that my classpath is wrong but i'm not entirely sure how to fix this either. This is what comes out when I type in echo $PATH
/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/home/taka/.rvm/bin
and when I type echo $CLASSPATH it doesn't show anything.
I have also tried java -cp ./ main.java as that seemed to have worked when I compiled and ran HelloWorld.java
If your javac is successful then update your classpath environment variable and add current directory i.e. . in the classpath, then run the java as below:
java main
Please note: There is no .java extension as you need to run .class file(which was generated after javac) that also without mentioning the extension. Java uses generated class files to execute not the original source files.
main.java java is your source code . you cant run java source without compile. For compilation you should use javac command. After that it will create a main.class file which can understand by interpreter which is java.
So you to run your class use java main or java main.class

Categories