import java.io.*;
public class WriteFile {
public static void main(String[] args){
try {
FileWriter writer = new FileWriter("Test.txt");
writer.write("this is a plain text file.\n");
writer.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
错误: 找不到或无法加载主类 O:WriteFile
原因: java.lang.ClassNotFoundException: O:WriteFile
This code can run on both eclipse and vs Code but not on Coderunner. How to solve it.
Your classpath is broken.
Method #1
Try adding the classpath while running it.
On windows:
java -classpath .;yourjar.jar YourMainFile
On Unix:
java -classpath .:yourjar.jar YourMainFile
Method#2
Configure the build path in your IDE and add an external JAR containing your class to the build path.
Method#3
Please read this to resolve the issue.
Note:
You could also refer to this.
Related
In eclipse for windows, when I run
public class HelloWorld {
public static void main(String[] args) {
System.out.println(System.getProperty("user.dir"));
}
}
It gives me the path of the project root folder (which contains the bin folder which has the class file). For example
SampleProject
and the class file is actually located at
SampleProject\bin\myclass.class
But if I run the same program in linux with
javac myclass.java
java myclass
it gives me the directory that has the .class file, which is the same as pwd command. This is what I want in eclipse for windows. I want some code that will give me the path to the class file in both eclipse for windows and linux.
Does anyone know how do this?
Thanks
If I understand you correctly, you'd like a method that retrieves a class' path on disk. This is easily achievable, like so:
public String getClassPath(Class c) {
try {
return c.getProtectionDomain().getCodeSource().getLocation().toURI().getPath();
} catch (URISyntaxException e) {
e.printStackTrace();
}
return null;
}
NOTE this will work even if the class is contained in a jar file. It will return the path to the jar in this case.
The easiest way is to do this:
public class HelloWorld {
public static void main(String[] args) {
System.out.println(HelloWorld.class.getResource("HelloWorld.class"));
}
}
I have the following code:
import java.sql.*;
public class ImageDB
{
public static void main(String[] args)
{
try
{
Class.forName("com.ibm.db2.jcc.DB2Driver");
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
My CLASSPATH has the db2jcc.jar:
.;
E:\All_Junk\DB2\java\db2java.zip;
E:\All_Junk\DB2\java\db2jcc.jar;
E:\All_Junk\DB2\java\sqlj.zip;
E:\All_Junk\DB2\java\db2jcc_license_cu.jar;
E:\All_Junk\DB2\BIN;
E:\All_Junk\DB2\java\common.jar
and related paths from my Path variable:
C:\Program Files\Java\jdk1.8.0_45\bin;
C:\Program Files\ibm\gsk8\lib64;
C:\Program Files (x86)\ibm\gsk8\lib;
E:\All_Junk\DB2\BIN;
E:\All_Junk\DB2\FUNCTION;
E:\All_Junk\DB2\samples\repl;
E:\All_Junk\DB2\java
So my question is: why am I still getting the error? Everything I've seen regarding this error has said that updating the CLASSPATH and/or Path variables will fix this, however Java is still unable to find the driver class.
did you try adding the Driver to the Java project build path and see if it can find that one.
I've tried this code and added the needed jar files but still I'm getting an error message like Exception in thread "main" java.lang.UnsatisfiedLinkError: Unable to load library 'libtesseract302'.
Is there a complete tutorial how to extract text and what things should be done to address the error? Any help is appreciated...
import net.sourceforge.tess4j.*;
import java.io.File;
public class ExtractTxtFromImg {
public static void main(String[] args) {
File imgFile = new File("C:\\Documents and Settings\\rueca\\Desktop\\sampleImg.jpg");
Tesseract instance = Tesseract.getInstance(); // JNA Interface Mapping
// Tesseract1 instance = new Tesseract1(); // JNA Direct Mapping
try {
String result = instance.doOCR(imgFile);
System.out.println(result);
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
}
In addition to adding the jars, you also need to add the natives. You can do so with Djava.library.path="C:\[absolute path to dir containing *.dll files and such]"
Note that you need to provide the directory, not the file itself.
I'm trying to run a Java program from another Java application. Here is my code:
public class Main {
public static int Exec() throws IOException {
Process p = Runtime.getRuntime().exec("javac -d C:/Users/Dinara/Desktop/D/bin "
+ "C:/Users/Dinara/Desktop/D/src/test.java");
Process p1 = Runtime.getRuntime().exec("java -classpath C:/Users/Dinara/Desktop/D/bin test");
return 0;
}
public static void main(String[] args) throws IOException {
Exec();
}
}
javac works fine and creates test.class file in bin directory. However java -classpath C:/Users/Dinara/Desktop/D/bin test does not run the test.class file.
the content of the test.java:
import java.io.*;
class test {
public static void main(String args[]) {
try {
FileWriter fstream = new FileWriter("out.txt");
BufferedWriter out = new BufferedWriter(fstream);
out.write("Hello Java");
out.close();
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
}
}
}
I suppose that something wrong with recognizing Java command. Could you please give me a sample code for fixing this problem or share idea? I'm using Netbeans to run Main class and the location of the application folder is C:\Users\Dinara\Main
Use
System.getProperty("java.home") + "/bin/java -classpath C:/Users/Dinara/Desktop/D/bin test"
instead of
"java -classpath C:/Users/Dinara/Desktop/D/bin test"
You need to supply the full path to the javac, exec won't use the ath to find it for you
I would like to determine the jar file name from my java code. I found many solutions in the google, but nothing works. Just to see what I tried here is a stackoverflow forum where a bunch of solutions is posted: stackoverflow
I have Mac OS X 10.6.5.
When I type java -version I get this result:
java version "1.6.0_22"
Java(TM) SE Runtime Environment (build 1.6.0_22-b04-307-10M3261)
Java HotSpot(TM) 64-Bit Server VM (build 17.1-b03-307, mixed mode)
Thank you for your help.
Edit:
I edit my post to answer for the comment.
Some of the solutions gives me "null" value when I want to System.out.println the path and also fails when I want to create an instance of a File.
Other solutions when I ask for the path they don't give something like file:/....., instead they give something like rsch:/ or something like, this I don't know exactly, but it is a 4 character simple word.
Edit 2:
I run an executable jar from the console. And I would like to have this jar file name in the classes which are in the executed jar file.
Edit 3:
The 4 character word is: rsrc:./
Code how I got this:
File file = null;
try {
System.out.println(MyClass.class.getProtectionDomain().getCodeSource().getLocation().toURI());
} catch (URISyntaxException e) {
e.printStackTrace();
}
Edit 4:
I also tried this code:
package core;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class MyClass {
public String getText(String key) {
String path = "" + MyClass.class.getResource("../any.properties");
File file = new File((path).substring(5, path.length()));
Properties props = readProps(file);
return props.getProperty(key);
}
private Properties readProps(File file) {
Properties props = new Properties();
InputStream in = null;
try {
in = new FileInputStream(file);
props.load(in);
in.close();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return props;
}
public static void main(String[] args) {
System.out.println(new MyClass().getText("anything"));
}
}
With this result:
Exception in thread "main" java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)
Caused by: java.lang.StringIndexOutOfBoundsException: String index out of range: -1
at java.lang.String.substring(String.java:1937)
at core.PropHandler.getText(MyClass.java:14)
at core.PropHandler.main(MyClass.java:39)
... 5 more
This code perfectly runs in the eclipse, but when I create the runnable jar file I think you can see the problem.
Is this what you want? http://www.uofr.net/~greg/java/get-resource-listing.html
jcomeau#intrepid:/tmp$ cat test.java; javac test.java; jar cvf test.jar test.class; java -cp test.jar test
public class test {
public static void main(String[] args) {
System.out.println(test.class.getResource("test.class"));
}
}
adding: META-INF/ (in=0) (out=0) (stored 0%)
adding: META-INF/MANIFEST.MF (in=56) (out=56) (stored 0%)
adding: test.class (in=845) (out=515) (deflated 39%)
Total:
------
(in = 885) (out = 883) (deflated 0%)
jar:file:/tmp/test.jar!/test.class
For access to resources that works regardless of the presence of a jar file, I always use classname.class.getResourceAsStream(). But the linked document shows how to use JarFile() for the same purpose.
Ok, finally this is the code which resolved my problem:
String sConfigFile = "any.properties";
InputStream in = MyClass.class.getClassLoader().getResourceAsStream(sConfigFile);
if (in == null) {
System.out.println("ugly error handling :D");
}
Properties props = new java.util.Properties();
try {
props.load(in);
} catch (IOException e) {
e.printStackTrace();
}
With this way it founds my property file.