Unable to run java program in current directory - java

I am trying to run a java program in current directory
I executed the ff. commands in current directory where the class file is HelloJNI.class, but it just doesn't work
java HelloJNI
and
java -cp . HelloJNI
Both commands doesn't work.
I also tried setting the CLASSPATH variable and it still doesn't work. Searched a lot but still have no idea.
The error says could not find class HelloJNI. I really can't understand why
------------- EDIT -------------------
package com.greetings;
public class HelloJNI
{
public static void main(String[] args)
{
System.out.println("Whatever");
}
}
adding the package name also results in same error:

Did you compile the Java code? For example:
javac
javac HelloJNI.java
Then you can run (if there are no compile errors).
java HelloJNI
I suggest using Maven or Gradle to build, test, lint etc your Java code. It will help greatly with dependency management and with a reproducible build on other operating systems and on CI/CD systems such as Jenkins and GitLab.

Related

How to run a simple java class on the command prompt in windows 8

I'm having a bit of trouble trying to run a java class on the command prompt. Its a very simple class and a friend of mine says it could be a problem with windows 8. Are there any suggestions. Here i'll show you the class and how I tried to compile it. I works fine in eclipse.
package gmit;
public class A {
public static void main(String[] args) {
System.out.println("hello");
}
}
In the command prompt I wrote
C:\Users\eclipse\workspace\Oct1stcasting\src\gmit>
followed by - javac A.java
java A.class
I tested the class using type A.java
and the text from the class does come up.
What am I doing wrong? Any help would be great.
If it runs in an Eclipse project, but not from command line, you could try this:
C:\Users\eclipse\workspace\Oct1stcasting\bin\gmit>java A
Inside a project directory, Eclipse saves source codes in /src and bytecodes in /bin. Thus, if you simply want to run your bytecode from command line, changing directory to /bin might suffice.
To compile
C:\Users\eclipse\workspace\Oct1stcasting\src\gmit>javac A.java
To run
C:\Users\eclipse\workspace\Oct1stcasting\src\gmit>java A
Don't append the .class extension while running the java program
Don’t add a .class suffix when using the java command.
Use java A or java -cp . A. The latter is required if the current directory is not implicitly used as class path.
Compilation:
C:\Users\eclipse\workspace\Oct1stcasting\src>javac gmit/A.java
Executing:
C:\Users\eclipse\workspace\Oct1stcasting\src>java gmit/A

CMake to compile java code

Is it possible to use CMake to compile and run java code?
From command line the commands that I write on terminal is:
javac -classpath theClasspath mainClass.java
java -classpath theClasspath mainClass
If so, could you please give me an idea of how this can be achieved?
PS: I do not want to generate a jar file; just to compile the java class and if possible to run it.
Thanks.
Update: I have changed the command. I do not know why the additional text was not displayed. It might be because I used "<" and ">".
CMake has somewhat limited support for compiling Java code and executing Java class files.
The standard module FindJava can be used to find a JDK installed on the local machine. The standard module UseJava provides a few functions for Java. Among those is a function add_jar to compile Java source files to a jar file.
Here is a small example that demonstrates how to use add_jar. Given the Java sample source file HelloWorld.java:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
The following CMake list file will compile HelloWorld.java to a jar file HelloWorld.jar and also add a CMake test that runs the jar with the JVM:
cmake_minimum_required (VERSION 2.8)
find_package(Java REQUIRED)
include(UseJava)
enable_testing()
project (HelloWorld NONE)
set(CMAKE_JAVA_COMPILE_FLAGS "-source" "1.6" "-target" "1.6")
add_jar(HelloWorld HelloWorld.java)
get_target_property(_jarFile HelloWorld JAR_FILE)
get_target_property(_classDir HelloWorld CLASSDIR)
message(STATUS "Jar file ${_jarFile}")
message(STATUS "Class compiled to ${_classDir}")
add_test(NAME TestHelloWorld COMMAND ${Java_JAVA_EXECUTABLE} -cp ${_jarFile} HelloWorld)
The CMake variable CMAKE_JAVA_COMPILE_FLAGS can be used to specify compile flags. As a side effect the add_jar command will set target properties JAR_FILE and CLASSDIR that can be used to obtain the path to the generated jar file and the compiled class files directory, respectively.

NoClassDefFoundError error while running JAVA console app

I created little HelloWorld example and I am having problem running it from command prompt (on Windows). When I attempt to run it by:
java tcpServer from command prompt I get NoClassDefFoundError
I am able to compile it with javac, and class file gets generated.
Somewhere I was reading that I have to put path to my class folder into CLASSPATH environment variable. I've done it and rebooted machine, but I still get the same error.
I also was trying to run it by java -cp c:\MyFolderWhereClassFileIs HelloWorld, it doesn't work.
I've looked into ENV variables and I have following:
JAVA_HOME: C:\Program Files (x86)\Java\jdk1.6.0_26;
JRE_HOME:C:\Program Files (x86)\Java\jre6;
CLASSPATH: C:\HelloWorld;
So, how do I run this?
Any ideas how to resolve this? Thanks.
PS. The most annoying thing to me is, if I create java project in eclipse, and create HelloWorld example, then it runs fine...
UPDATE:
Here is the code. It does have package specified.
package test.com;
public class HelloWorld {
public static void main(String[] args) {
System.out.print("Hello World");
}
}
My HelloWorld.java and HelloWorld.class files are here:
C:\workspace\TestApp\src\test\com
One thing I learned so far is that I can't run it from within com folder or test folder. I have to be in src folder class file can be found... but I am still not able to run it... always the same error.
Does your class have a package name? That is, a statement at the top which reads
package <mypkgname>;?
If so, then you have to (A) create the correct directory structure and (B) prefix the class name with the package name in your java command.
For example, if your Java file looks like this:
package hello;
public class HelloWorld {
...
}
Then a basic directory structure would resemble:
src/hello/
src/hello/HelloWorld.java
src/hello/HelloWorld.class
And your bash command would be:
cd src
java hello.HelloWorld
Otherwise, if your class has no package name, it will be placed in the default package. In this case, you simply have to cd to the directory where your class file resides, and run java HelloWorld.

Running a Java Class in Ubuntu with Virtual Box

Since I'm quite new in Java i have a question. As told on the title , I run an Ubuntu Server on VirtualBox and I have a problem running a very simple class with the use of package.
I give you the code:
package world;
public class HelloWorld{
public static void main (String[] args){
System.out.println("Hello World")
}
}
Very simple code indeed. After compiling it with javac HelloWorld.java, with no mistakes (ok now what mistakes could possible find),
Running java HelloWorld, gives me the message NoClassDefFoundError
Running java world.HelloWorld returns cannot find or load main class.
I suspect that it has something to do with classpath , but I cannot find an answer.
It's a classpath issue. You can probably check to see what your classpath is by looking at the CLASSPATH environment variable. You can try adding the directory your classfiles are at to the end of this CLASSPATH, but the simplest thing to do is probably the following.
Make sure the HelloWorld.java file is in a directory called world, and you can compile is like:
javac world/HelloWorld.java
This will create a HelloWorld.class file in the world directory. You can then try running
java world.HelloWorld
or
java -classpath . world.HelloWorld
from the same place.
You can also use the -d flag with javac to put the class files in a different place instead of the same place the source (.java files) are.

How do I compile my Java source files?

My source files are in this folder: c:\data\mycompany.
All of my source files contain the following as the first line: package mycompany; Now from the c:\data folder, I compiled everything using this command: javac mycompany/*.java -extdirs c:\some\other\folder\with\libs. This compiles fine. Now when I try to execute it (again from c:\data) using this command: java mycompany/test then i get this error:
Exception in thread "main"
java.lang.NoClassDefFoundError:
mycompany/test Caused by:
java.lang.ClassNotFoundException:
mycompany.test at java.net.URLClassLoader$1.run(Unknown Source)
I also tried the below command but it reproduces the same error:
java mycompany/test -extdirs c:\some\other\folder\with\libs
Is this the proper way to compile/run?
Here is my source-code:
package MyCompany;
public class Test
{
public static void main(String[] args)
{
System.out.println("test");
}
}
You need to set the classpath. See for example this SO question.
You shouldn't be using extdirs when invoking java, you should be setting your classpath with -cp
By the way, when invoking a main java class, you should provide the class name, not the path to the class, hence, it's likely mycompany.test (if your class that contains main is named test), not mycompany/test. It's not an error as Java fixes it for you.
that is saying that the .class files are not on the classpath how you are compiling should be fine, you need to add the directory with the resulting .class files to your classpath when you try and run your test code.
java -cp <path to classes> classtorun
so your example might look like
java -cp <path to classes>;<path to libs> mycompany.Test
you should really look at ANT to automate your compile and build to an executable .jar file. Nobody does this fiddly stuff by hand because of all the repetitive typing and chances for errors. ANT was created to avoid all this minutia and let you concentrate on solving coding problems and not struggling with the command line tools. Read this.
Try this to compile:
mkdir classes
javac -d classes *.java
Only create the /classes directory the first time. The -d directory tells javac to put your .class file under /classes.
To run, do this:
java -cp .;classes MyCompany.Test
This should work fine.

Categories

Resources