I am trying to run a simple Java program in Atom, with no success.
My code:
public class Hello {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
I use the cmd-i shortcut to run the code, and get the following error:
javac: invalid flag: /Users/xxxxx/Desktop/Programming/Atom (text editor)/AtomPrograms/AAA first test
Usage: javac
use --help for a list of possible options
[Finished in 0.586s]
The file did not have the same name as the class name.
It runs now.
Thank you
Related
Every time I try running a Java program through my command line I get this:
error(Error: could not open `C:\Program Files\Java\jre1.8.0_191\lib\amd64\jvm.cfg').
I'm just learning. This is my code I'm trying to run:
class cows2{
public static void main(String[] args){
System.out.println("Hello Friend");
}
}
Here is my command line with the error.
I have been trying to run Java with command line arguments, but for some reason the class can not be found. I am very certain the directory is correct. Is there any way to fix this?
CLDemo.java file
public class Demo {
public static void main(String[] args) {
System.out.print("It works!!!");
}
}
You need to do cd out\production before java CLDemo.
The default compile output of IntelliJ IDEA is under out\production folder, and Java needs to run at the corresponding package (folder) of your compile output.
I have written a java class
public class Uploadfiles {
public static void main(String[] args) throws InterruptedException {
System.out.println(args[0]);
//uploadFile(args[0]);
}
I have converted it to jar by right click project>Export> Runable jar> Package required libraries into generated Jar.
I have run the command to execute through command line
java -classpath JavaSeleniumLibs.jar javaSeleniumClasses.Uploadfiles Test
The output is
Test
I wanted to run the same commandline script through python and i use the method below
import os
def runCommandline():
print os.system("java -classpath JavaSeleniumLibs.jar javaSeleniumClasses.Uploadfiles Test")
I wanted the output "Test" to be printed as when done from commandline. How to do it?
How to return value from the executed Java function? like "The script is successfully executed"? Can I return other objects from the java function to the python os.system command?
I have the following helloworld class
class HelloWorld {
public static void main (String args[]) {
System.out.println("Hello World");
}
}
This is in an ASCII streamfile on the IFS called helloworld.java. When I try and compile this in QSH I get the following error
javac helloworld.java
helloworld.java:2: ')' expected
public static void main (String args[]) {
¢
I can't see a missing ')' in line 2. I suspect this is a codepage error because I've also never seen ¢ as placeholder on compile output.
Any ideas ?
Personally, I would suggest edit, compile, and test, your java code on a workstation (PC or Mac) and not trying to compile on the IBM i.
It's much more efficient that way.
Once you have code that works the way you want it to on your workstation, create a deployable JAR file and move that to the IBM i for further testing.
So when i try to open a java class that's not in a package from the command prompt it all works fine, but when I try to open a class that's in a package it gives me NoClassDefFoundError.
And when I list the package when I try to open the class (java somepackage/someclass) it says that it can't load or find the main class.
Any help?
What I can infer is, you have two classes:
Test.java:
// no package defined here
class Test{
public static void main(String[] args){
System.out.println("Test");
}
}
so you can compile and run it using:
javac Test.java
java Test
Another class:
Test.java:
package test; // package defined here
class Test{
public static void main(String[] args){
System.out.println("Test");
}
}
And thus doing same thing gives you error.
For this you need to be in the parent directory of 'test' folder in your terminal or cmd and use:
java test.Test
No problem with compiler. You can compile as usual using javac Test.java from inside 'test' folder.
NoClassDefFoundError means that your JVM can't find your class at runtime. This could be because it's not visible (set to private or protected, or just no access modifier).
It could also be missing from your build path
package pkg1;
public class Dataguise{
public static void main(String [] args){
System.out.println("My name is Maninder Singh");
}
}
Suppose this is my code. My package name is pkg1.
1. First you need to create pkg1 dirrectory if not existed.
2. Run "javac Dataguise.java" command
3. It will generate the "Dataguise.class" file and move this file to "pkg1" folder
4. Now run "pkg1.Dataguise" command it will work.
I was having the same issue so sharing my experience.
What I assume is, you are creating a package and a class inside it, for example.
package com.vishwa.hello.commandLineArgs;
public class ComandLineArguments {
public static void main(String[] args) {
// TODO Auto-generated method stub
for (int i = 0; i < args.length; i++) {
System.out.println("Command line arg is "+args[i]);
}
}
}
When I try to compile and run the above code in command prompt (from the package folder) as
javac ComandLineArguments.java.
java ComandLineArguments 123 456.
You get the below error:
Error: Could not find or load main class ComandLineArguments
Caused by: java.lang.NoClassDefFoundError: com/vishwa/hello/commandLineArgs/ComandLineArguments (wrong name: ComandLineArguments)
There are 2 ways to solve this:
Go to the root path of your project and then run.
java com/vishwa/hello/commandLineArgs/ComandLineArguments 123 456
789.
Command line arg is 123.
Command line arg is 456.
Command line arg is 789.
If you want to run the program from the package directory, you need to specify the class path along with the complete package
reference.
java -cp
/Users/16399/Documents/workspace-spring-tool-suite-4-4.12.1.RELEASE/CoreJava/src/
com/vishwa/ hello/commandLineArgs/ComandLineArguments 123 456 789.
Command line arg is 123. Command line arg is 456. Command line arg is 789.