Setting classpath enviroment variable value globally - java

I want to be able to execute a java class from Command Prompt in windows without specifying the classpath explicitly every time I want to execute a java class, for example like this
where I execute the HelloWorld class in the "ExamplePackage" package.
I want to accomplish the same result without specifying the classpath, as follows
but this gives me an Error: Could not find or load main class HelloWorld. As I understand it, this error is caused by java not being able to locate the class since classpath has not been specified.
Now, I want to solve this problem by setting a global variable classpath value to C:\Users\UpdatusUser\Desktop\ExampleProject.
This can be accomplished, as I understand, over here
But that does not seem to solve the issue, since I still get the following result if I now use java ExamplePackage.HelloWorld
What am I doing wrong?

For a full explanation see this:
What does "Could not find or load main class" mean?
The short answer is execute the java command from the ExampleProject folder not the ExamplePackage folder.

Related

java.lang.UnsatisfiedLinkError: com.example.program.ClassName.foo()L

I can't run methods of a library.
My library is in my PATH and also getting loaded without errors by following code:
System.loadLibrary("FTDIInterface");
But the functions are not working.
I get the following exception:
Caused by: java.lang.UnsatisfiedLinkError: Messgeraet.src.net.sf.yad2xx.FTDIInterface.getDevices()[LMessgeraet/src/net/sf/yad2xx/Device;
at Messgeraet.src.net.sf.yad2xx.FTDIInterface.getDevices(Native Method)
at Messgeraet.src.Emu.EmuConnection.<init>(EmuConnection.java:22)
at Messgeraet.src.Emu.EmuModel.connect(EmuModel.java:27)
at Messgeraet.src.JavaFX.FXController.connect(FXController.java:112)
... 62 more
I am using eclipse. In IntelliJ it is working fine and I also got another eclipse project that includes the library without any problems.
Why it can't run my method FTDIInterface.getDevices?
Your package seems off; Messgereat.src sounds like you have a project dir named Messgereat, within you have a folder named 'src' with your java sources, and you've misconfigured your build tooling; the right package name sounds like it should be: package net.sf.yad2xx;, but due to a misconfigured build it wasn't working and you decided to fix the problem by updating your package statements, but that broke your JNI bindings.
The solution would then be to undo all the changes you've made to your package statements, and fix your build script instead.
Alternatively, if you really do intend to use that bizarre package, then make sure you have executed javah with the exact same build setup and use that as a basis for your JNI code. If you've done that, include the exported symbols in the library as the comment by #user2543253 suggested.
NB: It's a bit odd that your loadLibrary call works at all; PATH has nothing to do with it, but presuambly then your library so happens to be located in a place that is listed on your librarypath, which is the system property (of the VM, not of your OS) named 'java.library.path'; you set it with e.g.:
java -Djava.library.path=/path1:/path2 -cp /path/to/dep1.jar:/path/to/dep2.jar com.foo.Main
because of this confusion it is also possible that some different native lib file also named FTDIInterface is being loaded instead of the one you think is being loaded. If you want to be certain of what is being loaded, run System.load("/absolute/path/to/the/dll-jnilib-or-so-libraryfile.so"); - then you know for sure.

Java files are not compliling

I have deploy my java code on AWS cloud, when I compile it on windows through terminal, I just have to use the command.
javac mainApp.java
it automatically create .class files of all other classes, lets say I have another class
class newProcessClass
who's variable is in mainApp.java, on EC2 when I compile it, it is giving me the error
error: could not find this symbol
newProcessClass npc = new newProcessClass();
same for the other classes. how can I compile it, and run it.
According to your description, I think that might be caused by classpath issue,
So please make sure current path(.) is under your classpath.
You can do this by type the follow into your terminal(pay attention to the little dot please!)
export CLASSPATH=.;$CLASSPATH
If you would like to persistent this setting and avoid set this everytime, you'd better add it to your .bashrc file.
And then when you run the class via java command, please also specify classpath as below
java -cp . mainApp
I recommend use Maven for the life cycle for application java and when you compiled the mainApp.java this action has that compile all file. you can get me more information of error stack?. For other side I think that is best that you use Maven. Best regards

NoClassDefFoundError and Could not find or load main class

I am using the code from Rome's tutorials page http://wiki.java.net/twiki/bin/view/Javawsxml/Rome05TutorialFeedReader . Also trying this one: http://wiki.java.net/twiki/bin/view/Javawsxml/Rome05TutorialFeedReader
Compiling works, but I'm not sure how to run these examples. Why I just type java FeedReader or java FeedAggregator into the command line, I get the error:
C:\projects\freshmeat\src>java FeedAggregator http://freecode.com/?format=atom
Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/syndication/f
eed/synd/SyndFeed
plus the large block that follows this error
Why is this happening, how do I fix it and try these things out? How do I get something to work with Rome!?
You need to include rome in the runtime classpath (in addition to the compile-time classpath)
java -classpath lib/rome.jar FeedAggregator ...
The samples you are trying to run are in the package com.sun.syndication.samples. You say you are a complete beginner, so, to make things simpler, I would recommend that you remove the line beginning with package in each of FeedReader.java and FeedAggregator.java. Recompile the classes after removing their package directives.
Then, to run these classes, make sure you're in the same directory as the class files FeedReader.class and FeedAggregator.class that javac created. Then, try running:
java -cp c:\projects\freshmeat\libs\rome-1.0.jar;c:\projects\freshmeat\libs\jdom-1.0.jar;. FeedReader
(and similarly for FeedAggregator.)
Note also that I've added the current directory, ., to the -cp attribute. Without this, the Java virtual machine won't know that it has to look in the current directory to find your FeedReader and FeedAggregator classes.
If you were to reinstate the package directives, you'd find the class files FeedReader.class and FeedAggregator.class would be created inside a directory com\sun\syndication\samples when you compile their sources. To run the class files from this location, you'd use a command line such as
java -cp c:\projects\freshmeat\libs\rome-1.0.jar;c:\projects\freshmeat\libs\jdom-1.0.jar;. com.sun.syndication.samples.FeedReader
and you'd run this from the directory containing the com subdirectory, not the directory that contains the class files.
More information on packages in Java can be found here.

Setting up test inputs in eclipse

I have some methods that would require to execute over a java class.
For example my method receives as argument a class file, something like:
Information info = grabInformation("class_to_execute");
This method would run the "class_to_execute"and capture its output. And I would like to later assert its output with a given expected value.
My question is: how could I set up eclipse so that my test cases would find the classes that it will execute? Is adding the classes to the build path enough? Are there some variables I could set?
I don't think the CLASSPATH has anything to do with it.
If "class_to_execute" is in another project or JAR, then add it to your Build Path under Libraries. Do you have any reason to believe that's not enough? Build path == CLASSPATH for most purposes.
If you're having Build Path or CLASSPATH problems, it might be easier to debug if you do this:
Information info = grabInformation(class_to_execute.class);
If it can't find the class, then put your cursor on the error and type Control+1. Eclipse might be able to help you fix the Build Path automatically.

NoClassDefFoundError on command-line with new NetBeans project

I've created a new J2SE project in NetBeans, and I can run it from the IDE, but when I try to run it using Ant on the command line, I get the following problem:
<snip>
run:
[java] Exception in thread "main" java.lang.NoClassDefFoundError: IndexBuilder
[java] Java Result: 1
<snip>
Based on the snippet from project.properties below, the class should be found.
run.classpath=\
${javac.classpath}:\
${build.classes.dir}
How do I go about fixing this?
The error you're getting means that one of the following is true:
The class IndexBuilder cannot be found on the classpath
A necessary (for class loading) dependency of IndexBuilder cannot be found on the classpath
That is, when loading the class, it's possible (even likely) that the class can be found but that some critical dependency of the class cannot be found. For example, if IndexBuilder extends another class and that base class cannot be found on the classpath, you'll get this error. Another example is if IndexBuilder uses a class in a static initializer and that class cannot be found.
Check your classpath not just for IndexBuilder but also for anything that IndexBuilder depends on.
See, for example, this discussion of NoClassDefFoundError.
When you are running it from the command line, you are actually invoking Apache Ant. The reason you are getting the ClassNotFound Exception is because ${javac.classpath} and all the other properties are not being properly populated. That is why your code runs from within the Netbeans context. Netbeans is setting those properties for you.
To answer your original question of how do you go about getting it to run from the command line, you need to either set up a properties file that defines those parameters via a property declaration:
<property file="myproject.properties"/>
Another solution is to set the properties as environment variables via a sh script. Or you can use real paths in the build script instead of properties.
See here for more details on how to invoke Ant from the command line.
Did you try setting the working directory to "build\classes" in the Project Properties -> Run tab?
At least one of the JARs/Libs referenced by your project may not be being copied to the class path of your program. Copy all of the jars/libs that your project uses to the /dist folder of your project (or wherever YourApplication.jar is), then try to run your program. If this fixes it it means your Netbeans project isn't configured quite correctly.
Are you running this on Windows or Unix. If Windows, try changing your property file to:
run.classpath=${javac.classpath};${build.classes.dir}
Please note the semicolon instead of a colon.

Categories