how to run java file in window powershell - java

I know there are similar questions out there but none of those solutions work for my case.
I tried to execute a Java file using Windows PowerShell.
This is what I have for Java file:
public class Demo1 {
public static void main (String arg[]){
System.out.println("DEMO1 START RUNNING");
try {
Thread.sleep(4000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("Unable to process");
}
System.out.println("DEMO1 JAVA COMPLETED.");
}
}
For easy compilation, I have copies of Demo1.java, Demo1.jar, and Demo1.class in the desktop directory.
for powershell command, I have tried:
1st attempt:
C:..\Desktop> java Demo1.java
2nd attempt:
C:..\Desktop> java -class Demo1.class
3rd attempt:
C:..\Desktop> java -jar Demo1.jar
All the above attempt return this error:
PS C:\Users\b003485\Desktop> java .\Demo1.java
Exception in thread "main" java.lang.NoClassDefFoundError: /\Demo1/java
Caused by: java.lang.ClassNotFoundException: .\Demo1.java
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: .\Demo1.java. Program will exit.

Related

Getting exception while using XSSFWorkbook - Exception in thread "main" java.lang.NoClassDefFoundError

While using XSSFWorkbook getting below exception, added org.apache.poi dependancy
my code is as below, it prints projDir and throws exception after that as mentioned below
public class ExcelUtils {
public static void main(String []args){
getRowCount();
}
public static void getRowCount(){
try {
String projDir =System.getProperty("user.dir");
System.out.println(projDir);
String excelPath = "./data/TestData.xlsx";
XSSFWorkbook workbook = new XSSFWorkbook(excelPath);
} catch (Exception exp) {
System.out.println(exp.getMessage());
System.out.println(exp.getCause());
exp.printStackTrace();
}
}
}
And below is full log track
Exception in thread "main" java.lang.NoClassDefFoundError: org/dom4j/DocumentException
at org.apache.poi.openxml4j.opc.OPCPackage.init(OPCPackage.java:149)
at org.apache.poi.openxml4j.opc.OPCPackage.<init>(OPCPackage.java:136)
at org.apache.poi.openxml4j.opc.Package.<init>(Package.java:54)
at org.apache.poi.openxml4j.opc.ZipPackage.<init>(ZipPackage.java:98)
at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:199)
at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:178)
at org.apache.poi.POIXMLDocument.openPackage(POIXMLDocument.java:62)
at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:188)
at utils.ExcelUtils.getRowCount(ExcelUtils.java:24)
at utils.ExcelUtils.main(ExcelUtils.java:11)
Caused by: java.lang.ClassNotFoundException: org.dom4j.DocumentException
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)

in java catch can't catch exception with jsoup

I want to parse a website with jsoup, and it succeeded to get the detail what I want, but I still get many many exceptions and not in catch
public static void main(String[] argv)
{
getGoogle();
}
private static void getGoogle() {
String url = "http://google.com";
Document doc = null;
try {
doc = Jsoup.connect(url).get();
Element body = doc.body();
}catch (Exception e) {
System.out.println("------------google------------");
System.out.println("getGoogle:" + e.getMessage());
}
}
when I use debug mode found it was because doc = Jsoup.connect(url).get(); this line, but it looks ok after I search much info, below is exceptions I got
Could not instrument class com/sun/deploy/security/MozillaJSSProvider: java.lang.IllegalArgumentException
at org.eclipse.jdt.launching.internal.org.objectweb.asm.ClassReader.<init>(Unknown Source)
at org.eclipse.jdt.launching.internal.weaving.ClassfileTransformer.transform(ClassfileTransformer.java:25)
at org.eclipse.jdt.launching.internal.javaagent.Premain$1.transform(Premain.java:51)
at java.instrument/java.lang.instrument.ClassFileTransformer.transform(ClassFileTransformer.java:246)
at java.instrument/sun.instrument.TransformerManager.transform(TransformerManager.java:188)
at java.instrument/sun.instrument.InstrumentationImpl.transform(InstrumentationImpl.java:550)
at java.base/java.lang.ClassLoader.defineClass2(Native Method)
at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1099)
at java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:206)
at java.base/jdk.internal.loader.BuiltinClassLoader.defineClass(BuiltinClassLoader.java:760)
at java.base/jdk.internal.loader.BuiltinClassLoader.findClassInModuleOrNull(BuiltinClassLoader.java:681)
at java.base/jdk.internal.loader.BuiltinClassLoader.findClass(BuiltinClassLoader.java:562)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:608)
at java.base/java.lang.Class.forName(Class.java:448)
at java.base/java.util.ServiceLoader.loadProvider(ServiceLoader.java:847)
at java.base/java.util.ServiceLoader.access$600(ServiceLoader.java:390)
at java.base/java.util.ServiceLoader$ModuleServicesLookupIterator.hasNext(ServiceLoader.java:1071)
at java.base/java.util.ServiceLoader$2.hasNext(ServiceLoader.java:1294)
at java.base/java.util.ServiceLoader$3.hasNext(ServiceLoader.java:1379)
at java.base/sun.security.jca.ProviderConfig$ProviderLoader.load(ProviderConfig.java:334)
at java.base/sun.security.jca.ProviderConfig$3.run(ProviderConfig.java:244)
at java.base/sun.security.jca.ProviderConfig$3.run(ProviderConfig.java:238)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at java.base/sun.security.jca.ProviderConfig.doLoadProvider(ProviderConfig.java:238)
at java.base/sun.security.jca.ProviderConfig.getProvider(ProviderConfig.java:218)
at java.base/sun.security.jca.ProviderList.getProvider(ProviderList.java:266)
at java.base/sun.security.jca.ProviderList.getIndex(ProviderList.java:296)
at java.base/sun.security.jca.ProviderList.getProviderConfig(ProviderList.java:280)
at java.base/sun.security.jca.ProviderList.getProvider(ProviderList.java:286)
at java.base/java.security.Security.getProvider(Security.java:470)
at java.base/sun.security.ssl.SignatureAndHashAlgorithm.<clinit>(SignatureAndHashAlgorithm.java:415)
at java.base/sun.security.ssl.SSLSessionImpl.<init>(SSLSessionImpl.java:182)
at java.base/sun.security.ssl.SSLSessionImpl.<init>(SSLSessionImpl.java:150)
at java.base/sun.security.ssl.SSLSessionImpl.<clinit>(SSLSessionImpl.java:79)
at java.base/sun.security.ssl.SSLSocketImpl.init(SSLSocketImpl.java:596)
at java.base/sun.security.ssl.SSLSocketImpl.<init>(SSLSocketImpl.java:534)
at java.base/sun.security.ssl.SSLSocketFactoryImpl.createSocket(SSLSocketFactoryImpl.java:72)
at java.base/sun.net.www.protocol.https.HttpsClient.createSocket(HttpsClient.java:413)
at java.base/sun.net.NetworkClient.doConnect(NetworkClient.java:162)
at java.base/sun.net.www.http.HttpClient.openServer(HttpClient.java:474)
at java.base/sun.net.www.http.HttpClient.openServer(HttpClient.java:569)
at java.base/sun.net.www.protocol.https.HttpsClient.<init>(HttpsClient.java:265)
at java.base/sun.net.www.protocol.https.HttpsClient.New(HttpsClient.java:372)
at java.base/sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(AbstractDelegateHttpsURLConnection.java:191)
at java.base/sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1181)
at java.base/sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:1075)
at java.base/sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:177)
at java.base/sun.net.www.protocol.https.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:163)
at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:651)
at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:676)
at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:676)
at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:628)
at org.jsoup.helper.HttpConnection.execute(HttpConnection.java:260)
at org.jsoup.helper.HttpConnection.get(HttpConnection.java:249)
at test3.test3.getGoogle(test3.java:23)
at test3.test3.main(test3.java:12)
Could not instrument class com/sun/deploy/security/MozillaJSSProvider$ProviderService: java.lang.IllegalArgumentException
at org.eclipse.jdt.launching.internal.org.objectweb.asm.ClassReader.<init>(Unknown Source)
at org.eclipse.jdt.launching.internal.weaving.ClassfileTransformer.transform(ClassfileTransformer.java:25)
Could not instrument class com/sun/deploy/security/MozillaJSSProvider$ProviderService: java.lang.IllegalArgumentException
at org.eclipse.jdt.launching.internal.org.objectweb.asm.ClassReader.<init>(Unknown Source)
at org.eclipse.jdt.launching.internal.weaving.ClassfileTransformer.transform(ClassfileTransformer.java:25)
Could not instrument class sun/security/pkcs11/SunPKCS11: java.lang.IllegalArgumentException
at org.eclipse.jdt.launching.internal.org.objectweb.asm.ClassReader.<init>(Unknown Source)
at org.eclipse.jdt.launching.internal.weaving.ClassfileTransformer.transform(ClassfileTransformer.java:25)
Could not instrument class sun/security/pkcs11/wrapper/PKCS11Exception: java.lang.IllegalArgumentException
at org.eclipse.jdt.launching.internal.org.objectweb.asm.ClassReader.<init>(Unknown Source)
at org.eclipse.jdt.launching.internal.weaving.ClassfileTransformer.transform(ClassfileTransformer.java:25)
Could not instrument class sun/security/pkcs11/SunPKCS11$Descriptor: java.lang.IllegalArgumentException
at org.eclipse.jdt.launching.internal.org.objectweb.asm.ClassReader.<init>(Unknown Source)
at org.eclipse.jdt.launching.internal.weaving.ClassfileTransformer.transform(ClassfileTransformer.java:25)
Could not instrument class com/sun/security/sasl/gsskerb/JdkSASL: java.lang.IllegalArgumentException
at org.eclipse.jdt.launching.internal.org.objectweb.asm.ClassReader.<init>(Unknown Source)
at org.eclipse.jdt.launching.internal.weaving.ClassfileTransformer.transform(ClassfileTransformer.java:25)
at org.eclipse.jdt.launching.internal.javaagent.Premain$1.transform(Premain.java:51)
Could not instrument class com/sun/security/sasl/gsskerb/JdkSASL$1: java.lang.IllegalArgumentException
at org.eclipse.jdt.launching.internal.org.objectweb.asm.ClassReader.<init>(Unknown Source)
at org.eclipse.jdt.launching.internal.weaving.ClassfileTransformer.transform(ClassfileTransformer.java:25)
at org.eclipse.jdt.launching.internal.javaagent.Premain$1.transform(Premain.java:51)
and more more exceptions.....and all not catch by try...catch
It is an eclipse bug, you can find the discussion here
Also, this has been raised in SO as well, this (https://stackoverflow.com/a/47109099/6039974) answer might help you.

javax.tools.JavaCompiler Setting up classpath

Im trying to write scripting system for a Minecraft Spigot plugin and im not quite sure on how to give my scripts the information from my main jar file.
With this current code it will compile the script fine but give me an error on line "3" of the Compass.java file (The class declaration). From what I see the program doesnt know what "com.deft.core.scripts.DeftScript" is and im not quite sure on how to tell it this information.
I got my compiling code from this thread if its relevant
Compiling the Script:
DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null);
List<String> optionList = new ArrayList<String>();
optionList.add("-classpath");
optionList.add(System.getProperty("java.class.path") + ";dist/Deft-Core.jar");
Iterable<? extends JavaFileObject> compilationUnit = fileManager.getJavaFileObjectsFromFiles(Arrays.asList(script));
JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, diagnostics, optionList, null, compilationUnit);
if (task.call()) {
Object obj = null;
try {
URLClassLoader classLoader = new URLClassLoader(new URL[]{new File("./plugins/Deft-Core/").toURI().toURL()});
Class<?> loadedClass;
loadedClass = classLoader.loadClass("scripts.Compass");
obj = loadedClass.newInstance();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
}
if (obj instanceof DeftScript) {
DeftScript deftScript = (DeftScript)obj;
deftScript.onEnable(this);
}
} else {
for (Diagnostic<? extends JavaFileObject> diagnostic : diagnostics.getDiagnostics()) {
System.out.format("Error on line %d in %s%n", diagnostic.getLineNumber(), diagnostic.getSource().toUri());
}
}
Compass.java
package scripts;
public class Compass extends com.deft.core.scripts.DeftScript {
#Override
public void onEnabled() {
}
}
DeftScript.java
package com.deft.core.scripts;
public abstract class DeftScript {
public abstract void onEnable();
}
With com.deft.core.scripts.DeftScript being the source im trying to extend from the main jarfile.
Im pretty sure my problem lies from somewhere within here:
List<String> optionList = new ArrayList<String>();
optionList.add("-classpath");
optionList.add(System.getProperty("java.class.path") + ";dist/Deft-Core.jar");
EDIT:
Fixed my problem with setting up the class path. My jar file in question was location in another location from what I specified.
I switch out the optionList.add calls with one
optionList.addAll(Arrays.asList("-classpath", System.getProperty("java.class.path") + ";./plugins/Deft-Core.jar"));
All tho this fixed my initial question now it as started causing a new error.
Apon trying to run my newly compiled class file it is giving me an error
java.lang.NoClassDefFoundError: com/deft/core/scripts/DeftScript
at java.lang.ClassLoader.defineClass1(Native Method) ~[?:1.8.0_45]
at java.lang.ClassLoader.defineClass(Unknown Source) ~[?:1.8.0_45]
at java.security.SecureClassLoader.defineClass(Unknown Source) ~[?:1.8.0_45]
at java.net.URLClassLoader.defineClass(Unknown Source) ~[?:1.8.0_45]
at java.net.URLClassLoader.access$100(Unknown Source) ~[?:1.8.0_45]
at java.net.URLClassLoader$1.run(Unknown Source) ~[?:1.8.0_45]
at java.net.URLClassLoader$1.run(Unknown Source) ~[?:1.8.0_45]
at java.security.AccessController.doPrivileged(Native Method) ~[?:1.8.0_45]
at java.net.URLClassLoader.findClass(Unknown Source) ~[?:1.8.0_45]
at java.lang.ClassLoader.loadClass(Unknown Source) ~[?:1.8.0_45]
at java.lang.ClassLoader.loadClass(Unknown Source) ~[?:1.8.0_45]
at com.deft.core.main.DeftCore.onEnable(DeftCore.java:79) ~[?:?]

Exception java.lang.NoClassDefFoundError android/app/Activity

In an android app, having:
public class Pars extends Activity{
public Document docout(){
InputStream is = getResources().openRawResource(R.raw.talechap01);
Document docout = null;
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = null;
try {
builder = domFactory.newDocumentBuilder();
docout = builder.parse(is);
} catch (ParserConfigurationException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return docout;
}
and
public class Manager {
public static String getStory(String spage, Document docs) {
XPathExpression expr;
XPath xpath = XPathFactory.newInstance().newXPath();
Object result = null;
String num = spage;
String par = null;
try {
expr = xpath.compile("//decision"+num+"/p//text()");
result = expr.evaluate(docs, XPathConstants.NODESET);
}
catch (XPathExpressionException e) {
e.printStackTrace();
}
NodeList nodes = (NodeList) result;
for (int i = 0; i < nodes.getLength(); i++) {
par = nodes.item(i).getNodeValue();
System.out.println(par);
}
return par;
}
}
Now I want to test the code.
public class Test {
public static void main(String[] args) {
try {Pars p = new Pars();
Document doc = p.docout();
System.out.println(Manager.getStory("000", doc));
}catch (Exception e){
e.printStackTrace();
}
}
}
I run "Test" as a java application and I get:
Exception in thread "main" java.lang.NoClassDefFoundError:
android/app/Activity at java.lang.ClassLoader.defineClass1(Native
Method) at java.lang.ClassLoader.defineClass(Unknown Source) at
java.security.SecureClassLoader.defineClass(Unknown Source) at
java.net.URLClassLoader.defineClass(Unknown Source) at
java.net.URLClassLoader.access$100(Unknown Source) at
java.net.URLClassLoader$1.run(Unknown Source) at
java.net.URLClassLoader$1.run(Unknown Source) at
java.security.AccessController.doPrivileged(Native Method) at
java.net.URLClassLoader.findClass(Unknown Source) at
java.lang.ClassLoader.loadClass(Unknown Source) at
sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at
java.lang.ClassLoader.loadClass(Unknown Source) at
com.stack.over.Test.main(Test.java:11) Caused by:
java.lang.ClassNotFoundException: android.app.Activity at
java.net.URLClassLoader$1.run(Unknown Source) at
java.net.URLClassLoader$1.run(Unknown Source) at
java.security.AccessController.doPrivileged(Native Method) at
java.net.URLClassLoader.findClass(Unknown Source) at
java.lang.ClassLoader.loadClass(Unknown Source) at
sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at
java.lang.ClassLoader.loadClass(Unknown Source) ... 13 more
I tried to find how to fix it but cannot achieve it.
I have this answer from another question but I'm kind of noob yet and can't figure it out how to fix it.
You have defined your Pars class as extending the class Activity. The
error you are seeing is telling you that the class
android.app.Activity is not available on the classpath. Maybe you need
to check which libraries you import. – adamretter Mar 5 at 10:01
Thanks all.
The error is that you're trying to run a program that references Android classes (android.app.Activity) as a regular Java program that doesn't have Android runtime libraries in its classpath.
You should run Android apps as Android apps (on an emulator or a device) and regular Java programs as regular Java programs and not mix the two. Activity is Android; main() method is regular Java.
To learn Android app development, you can start with the developer.android.com getting started documentation.
The error message says it all. You are writing Android now. Trying to write a main method doesn't make any more sense in Android than it would if you were writing a webapp that runs in Tomcat. You are going to need to step back and read up on how Android works.

NoClassDefFoundError while trying to use NxParser

I am trying to read a N-Triples (.nt) DBpedia file with NxParser, but I had the following error, and I don't know what to do.
Exception in thread "main" java.lang.NoClassDefFoundError: org/semanticweb/yars/nx/parser/NxParser$1
at org.semanticweb.yars.nx.parser.NxParser.stringItFromBufferedReader(Unknown Source)
at org.semanticweb.yars.nx.parser.NxParser.<init>(Unknown Source)
at org.semanticweb.yars.nx.parser.NxParser.<init>(Unknown Source)
at SentencesMatching_prova.main(SentencesMatching_prova.java:29)
Caused by: java.lang.ClassNotFoundException: org.semanticweb.yars.nx.parser.NxParser$1
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
... 4 more
The source code of the script is:
import java.io.*;
import org.semanticweb.yars.nx.parser.*;
public class SentencesMatching_prova {
public static void main(String[] args) {
try {
String relationFileName = "../zzz-trash/revisions_en.nt";
FileInputStream is = new FileInputStream(relationFileName);
NxParser nxp = new NxParser(is);
while (nxp.hasNext()) {
// do stuff
}
} catch (Exception ex) {
ex.printStackTrace(System.out);
}
}
}

Categories

Resources